对不可变的 JS Map 对象执行删除操作

Performing delete operations on an immutable JS Map object

我有一个不可变的 JS 映射,其结构如下所示

{
  "a": [
    {"name": "foo", "untracked": true},
    {"name": "bar"}
  ],
  "b": [
    {"name": "baz"},
    {"name": "bar", "untracked": true}
  ]
}

我想过滤此对象以仅显示被跟踪的对象 - 即

{
  "a": [
    {"name": "bar"}
  ],
  "b": [
    {"name": "baz"},
  ]
}

有没有办法用不可变的地图操作来做到这一点?我尝试了以下但它似乎不起作用

object.map((lis) => lis.filter((li) => li.untracked !== true)).toJS()

object.toList().map((lis) => lis.filter((li) => li.untracked !== true))

根据 ImmutableJS 文档:

filter() returns a new Map with only the entries for which the predicate function returns true.

示例:

function someFilterFunction(entry) {
  return !entry.untracked;
}
myMapOfStuff = myMapOfStuff.filter(someFilterFunction);

Link 到文档:https://facebook.github.io/immutable-js/docs/#/Map/filter

编辑:记住,不可变集合上的方法 returns 您正在操作的集合的副本。如果您想保留最新的参考,则必须进行重新分配。

遍历该对象并使用旧对象的键创建一个新对象,然后过滤未跟踪不等于 false 的旧对象的值

let obj = {
  "a": [{
      "name": "foo",
      "untracked": true
    },
    {
      "name": "bar"
    }
  ],
  "b": [{
      "name": "baz"
    },
    {
      "name": "bar",
      "untracked": true
    }
  ]
}
let newObj = {};

function filterObject(obj) {
  for (let keys in obj) {
    newObj[keys] = obj[keys].filter(item => {
      return item.untracked !== true
    })
  }
}
filterObject(obj)
console.log(newObj)

let data={
  "a": [
    {"name": "foo", "untracked": true},
    {"name": "bar"}
  ],
  "b": [
    {"name": "baz"},
    {"name": "bar", "untracked": true}
  ]
}
Object.entries(data).map(([key, value]) => {
  data[key]=value.filter(ele=>ele.untracked!=true)
});
console.log("result is : ",data)

data = {
  "a": [
    {"name": "foo", "untracked": true},
    {"name": "bar"}
  ],
  "b": [
    {"name": "baz"},
    {"name": "bar", "untracked": true}
  ]
}

let result = Object.entries(data).map(([key, values])=>(
 { key: values.filter(({untracked})=> !untracked) }
))

console.log(result)

const data = Immutable.fromJS({
  "a": [
    {"name": "foo", "untracked": true},
    {"name": "bar"}
  ],
  "b": [
    {"name": "baz"},
    {"name": "bar", "untracked": true}
  ]
});

const filteredData = data.map(item => item.filter(iItem => !iItem.get('untracked')));