欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS對象數(shù)組中如何匹配某個屬性值

 更新時間:2022年09月01日 11:14:42   作者:spe_track  
這篇文章主要介紹了JS對象數(shù)組中如何匹配某個屬性值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

對象數(shù)組中匹配某個屬性值

如果有一個js對象數(shù)組,一個變量如下

var a = [
?? ?{
?? ??? ?'id' : 2,
?? ??? ?'name' : 'xxxx',
?? ?},
?? ?{
?? ??? ?'id' : 3,
?? ??? ?'name' : 'bbbbb',
?? ?},
];
var b = 3;

我想要查詢 變量b,是否在數(shù)組a的id值中,如果在,返回true;不在,返回false

寫法如下

if (a.find(item => {return item.id == b}) != undefined)
{
? ? return true;
} else {
? ? return false;
}

find方法檢索數(shù)組,如符合條件 item.id == b,那么返回第一個被檢索到的對象,如果檢索不到,返回 undefined

對象數(shù)組取出對象屬性值完全一樣的項

vvar ary=[
?? ??? ??? ? ? ?{"RaDate":'2021-09-08',"Type":'Morning2','title':'測試2'},
?? ??? ??? ? ? ?{"RaDate":'2021-10-08',"Type":'Morning2','title':'測試1'},
?? ??? ??? ? ? ?{"RaDate":'2021-09-08',"Type":'Morning2','title':'測試2'},
?? ??? ??? ??? ?{"RaDate":'2021-10-08',"Type":'Morning2','title':'測試2'},
?? ??? ??? ? ? ]
?? ??? ??? ?let obj = {};
?? ??? ??? ?var result = ary.reduce((cur, next,index) => {
?? ??? ??? ? ? ?var key = next.RaDate + next.Type+ next.title
?? ??? ??? ? ? ?obj[key] ?cur.push(next): obj[key] = true
?? ??? ??? ? ? ?return cur;
?? ??? ??? ?}, [])
?? ??? ??? ?console.log(result)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論