【发布时间】:2019-07-18 09:45:20
【问题描述】:
我有一个要过滤的对象数组,然后返回过滤后的数组,然后与另一个数组进行比较,看看是否需要替换它。
例如,我想通过category_id = 2 过滤allItems,然后基于newItems 数组,我想返回过滤后的数组,但更新项在newItems(如果存在)。
allItems = [
{
'id': 1,
'category_id': 2,
'text': 'old',
'child': [
{
'id': 2,
'category_id': 2,
'text': 'old'
}
]
},
{
'id': 3,
'category_id': 3,
'text': 'old'
}
]
newItems = [
{
'id': 2,
'category_id': 2,
'text': 'new'
}
]
所以我想返回:
results = [
{
'id': 2,
'category_id': 2,
'text': 'new'
},
{
'id': 3,
'category_id': 2,
'text': 'old'
},
]
现在,我有过滤部分,但我不确定如何与另一个数组进行比较并替换为更新的项目。
return this.allItems.filter( (item) => {
return item.category_id == 2
})
【问题讨论】:
标签: javascript