如何检查相同的 属性 存在于单个对象数组 javascript 中
How to check same property exists in single array of object javascript
我想知道对象数组在 javascript
中是否具有相同的 属性 值
为数组对象list1
,
if name and country has same value, return true
if name same, any object country has value SG return true
if above two conditions fails, return false
var list1=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);
var list2=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);
var list3=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);
Expected Result:
//when passing list1
true
//when passing list2
true
//when passing list3
false
试试这个,
var list1=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);
console.log(result1);
var list2=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);
console.log(result2);
var list3=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);
console.log(result3);
function checkObj(list){
for(i=0;i<list.length;i++)
{
for(j=0;j<list.length;j++)
{
if(j!=i){
if(list[i].name==list[j].name){
if(list[i].country==list[j].country || list[i].country =='SG'){
return true;
}
}
}
}
}
return false;
}
在下面的代码片段中,我汇总了一种基于对数组的所有元素进行单循环的方法。 name
/country
组合的出现被收集在本地对象 o
中,并在函数结束时进行检查。
const data=[[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
],[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"},
{id:1, name: "sen", country: "DN"},
{id:1, name: "sen", country: "EN"}
],[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]];
function dupes(arr){
const o={}, // object o counts all occurrences
// the sort makes sure the "SG" items come last in ar
ar= arr.slice(0).sort((a,b)=>a.country==="SG" ? (b.country==="SG" ? 0 : 1) : -1);
// a single loop over ar:
ar.forEach(e=>{
if(e.country!=="SG"){ // "normal" countries:
let k=e.name+"|"+e.country;
o[k]=(o[k]||0)+1
} else { // for country=="SG"
for (let k in o)
if (k.indexOf(e.name+"|")==0)
o[k]++ // increase counts for all elements starting with <el.name>|
}
});
return Object.values(o).some(n=>n>1)
}
data.forEach(d=>console.log(dupes(d)))
我想知道对象数组在 javascript
中是否具有相同的 属性 值为数组对象list1
,
if name and country has same value, return true
if name same, any object country has value SG return true
if above two conditions fails, return false
var list1=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);
var list2=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);
var list3=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);
Expected Result:
//when passing list1
true
//when passing list2
true
//when passing list3
false
试试这个,
var list1=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);
console.log(result1);
var list2=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);
console.log(result2);
var list3=[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);
console.log(result3);
function checkObj(list){
for(i=0;i<list.length;i++)
{
for(j=0;j<list.length;j++)
{
if(j!=i){
if(list[i].name==list[j].name){
if(list[i].country==list[j].country || list[i].country =='SG'){
return true;
}
}
}
}
}
return false;
}
在下面的代码片段中,我汇总了一种基于对数组的所有元素进行单循环的方法。 name
/country
组合的出现被收集在本地对象 o
中,并在函数结束时进行检查。
const data=[[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "IN"}
],[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "SG"},
{id:1, name: "sen", country: "DN"},
{id:1, name: "sen", country: "EN"}
],[
{id:1, name: "sen", country: "IN"},
{id:2, name: "sen", country: "TH"}
]];
function dupes(arr){
const o={}, // object o counts all occurrences
// the sort makes sure the "SG" items come last in ar
ar= arr.slice(0).sort((a,b)=>a.country==="SG" ? (b.country==="SG" ? 0 : 1) : -1);
// a single loop over ar:
ar.forEach(e=>{
if(e.country!=="SG"){ // "normal" countries:
let k=e.name+"|"+e.country;
o[k]=(o[k]||0)+1
} else { // for country=="SG"
for (let k in o)
if (k.indexOf(e.name+"|")==0)
o[k]++ // increase counts for all elements starting with <el.name>|
}
});
return Object.values(o).some(n=>n>1)
}
data.forEach(d=>console.log(dupes(d)))