在另一个对象JavaScript中搜索多个对象值

时间:2017-11-06 18:48:16

标签: javascript jquery arrays lodash

我有2个对象数组,我想比较第二个对象数组中存在的所有第一个数组对象属性和值。 (在对象层面。)

数组1

 [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id3",
    "name": "name3"
  }
]

阵列2

[
  {
    "id": "id1",
    "name": "name1",
    "other": "other"
  },
  {
    "id": "id2",
    "name": "name2",
    "other": "other"
  },
  {
    "id": "id3",
    "name": "name3",
    "other": "other"
  }
]

在这种情况下,它将返回 true ,因为数组2中存在所有数组1的值。

但如果我有一个与数组2不匹配的属性值,则返回false。

 [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id5",
    "name": "name3"
  }
]

这将返回false,因为{" id":" id5"," name":" name3"}不存在于数组2。

我试图与两个for循环进行比较,但我想在两个循环完成比较所有属性和value之后只返回一次.now,

这是我尝试的两个循环,我知道这是错误的,因为当没有找到值时它将返回false并且它不会进入进一步的循环。

for (var i = 0,   i < a.length; i++) {
  for (var j = 0, j < b.length; j++) {
    if (b[j].id === a[i].id && b[j].name === a[i].name) {
      console.log('all values found');
      return true;
    }
    console.log('some values does not found');
     return false;
  }
}

我也在使用lodash。

3 个答案:

答案 0 :(得分:1)

如果对象属性具有相同的顺序,则可以解决您的问题:

const array1 = [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id2",
    "name": "name2"
  }
],
array2 = [
  {
    "id": "id1",
    "name": "name1",
    "other": "other"
  },
  {
    "id": "id2",
    "name": "name2",
    "other": "other"
  },
  {
    "id": "id3",
    "name": "name3",
    "other": "other"
  }
];

let present = true,
item,
property,
properties;

for (let i = 0; i < array1.length && present; i++) {
    item = array1[i];
    properties = Object.keys(item);

  for (let j = 0; j < properties.length; j++) {
    property = item[properties[j]];

    if (item.hasOwnProperty(properties[j])) {
      if (property !== array2[i][Object.keys(array1[i])[j]]) {
        present = false;
      } 
    }
  }
}

答案 1 :(得分:1)

使用lodash的_.intersectionBy(),并将结果的长度与较短数组的长度进行比较:

var arr1 = [{"id":"id1","name":"name1","other":"other"},{"id":"id2","name":"name2","other":"other"},{"id":"id3","name":"name3","other":"other"}];
var arr2 = [{"id":"id1","name":"name1"},{"id":"id3","name":"name3"}];
var arr3 = [{"id":"id1","name":"name1"},{"id":"id5","name":"name3"}];

var result1_2 = _.intersectionBy(arr2, arr1, 'id').length === arr2.length;

var result1_3 = _.intersectionBy(arr3, arr1, 'id').length === arr2.length;

console.log(result1_2);

console.log(result1_3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 2 :(得分:0)

非常简单,一种丑陋的方法是将对象转换为字符串并进行比较,但这需要您的对象具有相同的属性

 var a1 = [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id3",
    "name": "name3"
  }
];

var a2 = [
  {
    "id": "id1",
    "name": "name1",
  },
  {
    "id": "id2",
    "name": "name2",
  },
  {
    "id": "id3",
    "name": "name3",
  }
];

var a3 =  [
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id5",
    "name": "name3"
  }
];


var doesArrayBContainArrayA = function(a,b)
{
  var u = [];
  a.map(e => 
        {
          var match = false;
          b.forEach(function(bx){
            if(!match){
             match = JSON.stringify(bx) === JSON.stringify(e);  
            }
          });
          if (!match){ 
              //console.log(JSON.stringify(bx)+'--'+JSON.stringify(e));
              u.push(e);   //add non existing item to temp array
            }

  });
  return u.length === 0;
}
var result = doesArrayBContainArrayA(a3,a2);
var result2 = doesArrayBContainArrayA(a1,a2);
console.log(result);
console.log(result2);