在同一元素中连接2个句子ImmutableJS

时间:2018-12-18 19:53:56

标签: javascript dictionary ecmascript-6 immutability immutable.js

我正在尝试使用与当前元素相同的concat 2句子,并在每个不可变对象的末尾添加句点

这是输入内容和我当前的代码

const Immutable = require("immutable");
let error = Immutable.fromJS({
  name: ["This field is required", "Another error"],
  age: ["Only numeric characters are allowed"]
});

error.map((currElement, index) => {
console.log("The current iteration is: " + index);
console.log("The current element is: " + currElement);
});

我的预期输出是

 error = {
 name: "This field is required. Another error.",
 age: "Only numeric characters are allowed."
 };

尝试和错误尝试

error.map((currElement, index) => {
  // console.log("The current iteration is: " + index);
  // console.log("The current element is: " + currElement);

  let element = currElement.get(0) + "." + " " + currElement.get(1) + ".";
  return console.log(element);
});

关闭,但是我仍然无法获得正确的输出。

1 个答案:

答案 0 :(得分:1)

数组上的大多数方法都可以在不可变列表中找到,.join()是其中之一,因此您应该能够做到:

const newError = error.map((value) => {
  return value.map(v => `${v}.`).join(' ');
}).toJS(); // .toJS() if you want the object from your expected output

您可能还应该在对它运行.join()之前检查该值确实是List。