使用immutableJs更新OrderMap中具有特定值的所有键

时间:2018-04-27 04:23:29

标签: javascript-objects immutable.js

我试图找到使用immutableJs迭代所有键的更好方法。 (基本上,我想立即更新地图中的所有值。) 通过更改tdata.setIn([tdata.keySeq(),“comments”],value =>“new Comment”)来尝试; setIn,UpdateIn - Immutable中的深度持久更改方法

let data = [
  {
    id: "1",
    isSelected: false,
    isStarred: false,
    comments: “comments”,
    title: “Title1”,
    userName: “uName”
  },
  {
    id: "2",
    isSelected: false,
    isStarred: false,
   comments: “comments”,
       title: “Title1”,
    userName: “uName”
  },
  {
    id: "3",
    isSelected: false,
    isStarred: false,
    comments: “comments”,
    title: “Title1”,
    userName: “uName”
  },
  {
    id: "4",
    isSelected: false,
    isStarred: false,
    comments: “comments”,
    title: “Title1”,
    userName: “uName”
  },
  {
    id: "5",
    isSelected: false,
    isStarred: false,
    comments: “comments”,
    title: “Title1”,
    userName: “uName”
  }
]

let tData = OrderedMap();
   for (let i = 0; i < data.length; i++) {
   let values = Map(data[i]);
   tData = tData.set(data[i].id, values);

}

tdata.updateIn([“2”, "comments"], value =>”new Comment”);

我没有明确指定key =“2”,而是希望更新整个Map 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

听起来你想使用map功能。

const data = fromJS({
  a: {comment: "1"},
  b: { comment: "2"},
  c: {count: "3"}
);
const newData = data.map((key, value) => {
  if (value.get("comment") == "2")
    return value.set("comment", "new comment")
  else
    return value
});