【发布时间】:2020-07-07 10:59:54
【问题描述】:
我有一个如下数组:
selected = ["ID-1", "ID-2"];
还有一个对象数组,比如
objectArray = [
{
"id":"ID-1",
"description":"Desction goes here",
"another_filed":"Another Value"
},
{
"id":"ID-2",
"description":"Desction goes here",
"another_filed":"Another Value"
},
{
"id":"ID-3",
"description":"Desction goes here",
"another_filed":"Another Value"
}
]
现在我需要从具有id 的objectArray 中删除项目,它存在于selected 数组中。因此,在上述情况下,我需要从objectArray 中删除第一项和第二项,因为selected 变量中存在相应的 id,并且结果应该只有一个 id 值为 3 的对象。我怎么能这样做?
【问题讨论】:
-
objectArray = objectArray.filter(elem => !selected.includes(elem.id)); -
remove 是否意味着保持数组的相同对象引用?你试过什么吗?
标签: javascript arrays json typescript