我想定义一个函数来查找数组中 JSON 对象的索引。JSON 对象是动态的。这里对象键/属性在 JSON 中不是常量。
如何在数组中找到匹配对象(所有键和值)的索引?
例如:
let obj={name:'Bill', phone:'8562456871', email:'bill@email.com'};
let arrayObj=[{street:'wardcircle', city:'Brentwood'},{name:'wan',email:'wan@test.com' },{name:'bill', phone:'8562456871', email:'bill@email.com'}];
let indx=getIndex(obj,arrayObj); // expected result is 2
我已经定义了这样的函数,但它不适用于所有动态属性和值:
getIndex(obj,arrayObj){
Object.keys(obj),forEach((key,index)=>{
return arrayObject.findIndex(x=>x[key]==obj[key]);// Here I am unable to add AND condition for other key& values.
});
}
慕侠2389804
相关分类