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

Update all keys with particular value in OrderMap using immutableJs

我正在尝试找到使用 immutableJs 遍历所有键的更好方法。 (基本上,我想一次更新地图中的所有值。) 尝试通过更改 tdata.setIn([tdata.keySeq(), "comments"], value =>”new Comment”); setIn 、 UpdateIn - 不可变中的深度持久更改方法

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”);

我希望更新整个地图,而不是明确指定键 ="2" 感谢您的帮助

听起来您想使用 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
});