简体   繁体   English

比较两个对象数组,只得到数组的不同属性

[英]Compare two array of objects and get only the different Property of the array

Array1 = { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }; 

Array2 = { "Name": "Alex", "Occupation": "S.E", "Location": "KGP" }; 

In the above given 2 arrays i have different values in properties Name and Occupation but the location have the same value as that of Array1,在上面给出的 2 个数组中,我在属性 Name 和 Occupation 中有不同的值,但位置与 Array1 的值相同,

i want to compare the two arrays and get only the Different Property(Name and Occupation) of Array1我想比较两个数组并只获取 Array1 的不同属性(名称和职业)

You could iterate over the keys of object1 and check the difference with the properties of object2 .您可以遍历object1的键并检查与object2属性的差异。 If unequal, assign the value to the property of the result object3 .如果不相等,则将值赋给结果object3的属性。

 var object1 = { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }, object2 = { "Name": "Alex", "Occupation": "SE", "Location": "KGP" }, object3 = {}; Object.keys(object1).forEach(function (k) { if (object1[k] !== object2[k]) { object3[k] = object1[k]; } }); console.log(object3);

var obj1 = { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }; 

var obj2 = { "Name": "Alex", "Occupation": "S.E", "Location": "KGP" }; 

function getDifference(obj1,obj2){
  var diff = [];
  Object.keys(obj1).forEach(function(key){
  if (obj1[key] !== obj2[key]) diff.push(key);
  });
  return diff;
}
console.log(getDifference(obj1,obj2));

Your Array1 and Array2 are JavaScript Object .你的 Array1 和 Array2 是JavaScript Object With 2 objects, you can perform this working, but dirty comparison.使用 2 个对象,您可以执行此工作但比较脏的比较。 As soon as you will have more properties ... This will be a mess.一旦你有更多的属性......这将是一团糟。

   var result = {};

    if (Array1.Name !== Array2.Name)
       result.Name = Array1.Name;
    if (Array1.Occupation !== Array2.Occupation)
       result.Occupation = Array1.Occupation.

   return result;

Arrays in JavaScript are declared with the following syntax: JavaScript 中的数组使用以下语法声明:

var Array1 = [];

If you want an array of objects, you can write:如果你想要一个对象数组,你可以写:

var Array1 = [ { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }, { b"Name": "Max", "Occupation": "Agri", "Location": "KGP"} ]

But I doubt this is your intention there.但我怀疑这是你的意图。

If the objective is to get mismatching keys, you can try something like this:如果目标是获得不匹配的密钥,您可以尝试以下操作:

 function getMismatchKeys(o1,o2){ return Object.keys(o1).filter(function(k){ return o1.hasOwnProperty(k) && o2.hasOwnProperty(k) && o1[k] !== o2[k] }); } var obj1 = { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }; var obj2 = { "Name": "Alex", "Occupation": "SE", "Location": "KGP" }; console.log(getMismatchKeys(obj1, obj2));

I will assume that Array1 and Array2 are meant to be json objects and not json arrays.我将假设 Array1 和 Array2 是 json 对象而不是 json 数组。 I will also assume that you want a resulting object that is made up of only the key value pairs of Array1 which differ from Array2.我还将假设您想要一个结果对象,该对象仅由与 Array2 不同的 Array1 的键值对组成。 Given those assumption you could use lodash omitBy to solve your problem.鉴于这些假设,您可以使用lodash omitBy来解决您的问题。

 var Array1 = { "Name": "Max", "Occupation": "Agri", "Location": "KGP" }; var Array2 = { "Name": "Alex", "Occupation": "SE", "Location": "KGP" }; var result = _.omitBy(Array1, function(val, key){ return val === Array2[key];}); console.log('the key value pairs of Array1 that are different from Array2 are: ', result);
 <script src="https://cdn.jsdelivr.net/lodash/4.16.1/lodash.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM