【发布时间】:2019-12-05 19:38:55
【问题描述】:
我正在使用其他代码,试图不破坏应用程序中的其他功能,但基本上我需要在条件匹配时删除对象。
我之前问的问题会删除对象,但不会在不同的代码库中,所以希望有人能给我一些想法。
感谢您的帮助!
const data = {
'123': {
'name': 'Part 1',
'size': '20',
'qty' : '50'
},
'5678' : {
'name': 'Part 2',
'size': '15',
'qty' : '60'
},
'9810' : {
'name': 'Part 2',
'size': '15',
'qty' : '120'
},
}
// my code I tried work with:
const getValue = Object.keys(data).reduce((acc,id)=> {
const condition = ['9810','5678'];
if(acc[key]){
// remove the object if the key match
return !acc.includes(key[id])
} else {
// if not match just return the original object
return acc
}
},{})
【问题讨论】:
-
输出应该是什么?
-
另外,它是
Object.keys而不是Object.key。
标签: javascript arrays object reduce