gpt4 book ai didi

javascript - 循环遍历对象数组,如果属性匹配,则对该对象进行切片 - Javascript

转载 作者:行者123 更新时间:2023-11-28 12:17:34 27 4
gpt4 key购买 nike

我有对象数组,我正在尝试循环该数组,如果找到匹配项,我想对该对象进行切片..

var object = [
{
"Name": 'Kshitij',
"LastName": 'Rangari',
"CountryBorn": 'India',
"CountryStay": 'USA'
},

{
"Name": 'Pratik',
"LastName": 'Rangari',
"CountryBorn": 'India',
"CountryStay": 'Canada'
},

{
"Name": 'Pratibha',
"LastName": 'Rangari',
"CountryBorn": 'India',
"CountryStay": 'India'
},

{
"Name": 'Ankita',
"LastName": 'Raut',
"CountryBorn": 'India',
"CountryStay": 'Australia'
},

{
"Name": 'Wayne',
"LastName": 'Rooney',
"CountryBorn": 'UK',
"CountryStay": 'UK'
}

]

console.log(object);

object.forEach(function(x){
if (x.Name==='Kshitij'){

}
})

object.map (obj =>{

obj.AllFirstName = obj['Name'];
console.log(obj['AllFirstName']);

})

console.log('------------------------------')

console.log(object);

我想循环遍历对象并想查找是否 Name === 'Kshitij'Name ==='Pratik' ,我想从数组中删除这些对象。

我该怎么做?

最佳答案

您可以使用返回一个过滤数组,该数组过滤掉数组中对象的给定键的所有给定值。

const without = (key, values) => object => !values.includes(object[key]);

var array = [{ Name: 'Kshitij', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'USA' }, { Name: 'Pratik', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'Canada' }, { Name: 'Pratibha', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'India' }, { Name: 'Ankita', LastName: 'Raut', CountryBorn: 'India', CountryStay: 'Australia' }, { Name: 'Wayne', LastName: 'Rooney', CountryBorn: 'UK', CountryStay: 'UK' }];

console.log(array.filter(without('Name', ['Kshitij', 'Pratik'])));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 循环遍历对象数组,如果属性匹配,则对该对象进行切片 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45652369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com