Immutable.JS联系并修改输出

时间:2018-12-17 18:11:53

标签: javascript object mocha immutable.js purely-functional

在immutableJS中,我试图将输出传递给。

名称:“此字段为必填。另一个错误。”,   年龄:“仅允许使用数字字符。”

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

2 个答案:

答案 0 :(得分:1)

ONELINER :就地(this version of question的答案-示例)

Object.entries(error).map( x=> error[x[0]] = x[1].join('. ')+'.' );

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


Object.entries(error).map(x=>error[x[0]]=x[1].join('. ')+'.');
console.log(error);

通过这种方式,您可以将其包装在函数中(不可变):

let var func = (e,r={})=>(Object.entries(e).map(x=>r[x[0]]=x[1].join('. ')+'.'),r);
let result = func(error) // run it in this way

答案 1 :(得分:0)

您可以使用Array.reduce()

error.reduce((prev,next,key) => prev.concat(`${key}: "${next.join('. ')}". `),'')