我有两个对象数组
var arr1 = [
{id='1', text='xyz'},
{id='2', text='abcd'}
];
var arr2 = [
{id='1', text='xyz'},
{id='2', text='abcd'},
{id ='3', text='pqrs'}
];
我需要一种方法来通过' id'来获取两个数组的唯一性。并将它们放在arr3中,Array3应该是 - :
var arr3 = [{id ='3', text='pqrs'}];
答案 0 :(得分:1)
在for循环中,您必须根据长度明智选择大数组并尝试此
$( document ).ready(function() {
var arr1 = [
{id:'1', text:'xyz'},
{id:'2', text:'abcd'}
];
var arr2 = [
{id:'1', text:'xyz'},
{id:'2', text:'abcd'},
{id :'3', text:'pqrs'}
];
var arr3 =[];
for(var i = 0;i<arr2.length;i++)
{
var cnt =0;
$.each( arr1, function( k, v ) {
if(arr2[i].id == v.id && arr2[i].text == v.text)
{
cnt ++;
}
});
if(cnt == 0){arr3.push(arr2[i]);}
}
console.log(arr3);
});