我想過濾彈性查詢。我添加了項目,但我無法刪除項目。我的陣列在下面。例如,我想刪除數組中包含 item2 的元素。我想檢查 item2 鍵是否存在,如果存在則刪除該項目。我能怎麼做?
let array = [
{
terms: {
item1: [
'1111111111111',
'2222222222222',
'3333333333333',
'4444444444444'
]
}
},
{
terms: {
item2: [
'aaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbb',
'ccccccccccccccccccccccc'
]
}
},
{
range: {
item3: {
from: 0,
to: 99999
}
}
}
];
像這樣的東西?
let array = [{
terms: {
item1: [
'1111111111111',
'2222222222222',
'3333333333333',
'4444444444444'
]
}
},
{
terms: {
item2: [
'aaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbb',
'ccccccccccccccccccccccc'
]
}
},
{
range: {
item3: {
from: 0,
to: 99999
}
}
}
];
const filtered = array.filter((el) => !el[Object.keys(el)[0]].hasOwnProperty('item2'))
console.log(JSON.stringify(filtered))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句