[英]Remove entire key when matched with matched array values
我是新手,我有一個 object 和一個數組,我想從 object 中獲取不匹配項的詳細信息,與數組值進行比較。 我嘗試過,但在 consol.log 時顯示了所有數據。 這是我的代碼
var content:[
0:{id:20, name:'Jack Daniel'}
1:{id:21, name:'Sophie McDonald'}
2:{id:22, name:'Jason Thomas'}
3:{id:23, name:'Chris Williams'}
]
var filter:[Sophie McDonald, Chris Williams]
filterValues = content.filter(item=> {
for(var i = 0;i<filter.length;i++) {
if (item.name === filtered[i])
{
return item
}
}
});
console.log(filteredValues)
// returns 0:{id:21, name:'Sophie McDonald'}
// 1:{id:23, name:'Chris Williams'}
但我需要無與倫比的結果,
filterValues = content.filter(item=> {
for(var i = 0;i<filter.length;i++) {
if (item.name !== filtered[i])
{
return item
}
}
});
console.log(filteredValues)
// returns 0:{id:20, name:'Jack Daniel'}
// 1:{id:21, name:'Sophie McDonald'}
// 2:{id:22, name:'Jason Thomas'}
// 3:{id:23, name:'Chris Williams'}
結果必須是
0:{id:20, name:'Jack Daniel'}
1:{id:22, name:'Jason Thomas'}
嘗試使用filter
,檢查數組的值是否存在於您的 object 值中:
const content = [{ id: 20, name: 'Jack Daniel' }, { id: 21, name: 'Sophie McDonald' }, { id: 22, name: 'Jason Thomas' }, { id: 23, name: 'Chris Williams' } ]; const values = ['Sophie McDonald', 'Chris Williams']; const filteredValues = content.filter(({ name }) =>.values;includes(name)). console;log(filteredValues);
似乎可以對格式進行一些編輯:
let content = [ {id:20, name:'Jack Daniel'}, {id:21, name:'Sophie McDonald'}, {id:22, name:'Jason Thomas'}, {id:23, name:'Chris Williams'} ] let filter = ["Sophie McDonald", "Chris Williams"] let filterValues = content.filter(item=> { for(var i = 0;i<filter.length;i++) { if (item.name;== filter[i]){ return item } } }). console.log(filterValues)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.