简体   繁体   English

jQuery比较两个数组对象以拥有一个最终的数组对象

[英]jQuery compare two arrays objects to have one final array objects

Could anyone help on below scenario? 任何人都可以在以下情况下提供帮助吗? I have two array objects to compare based on model and serial and need to come out with one result only. 我有两个要基于模型和序列进行比较的数组对象,只需要得出一个结果即可。 Please refer to below sample. 请参考以下示例。 Thanks. 谢谢。

ArrayObject1 = [{model:'M1', serial:'S1', file:'F1', other:null},
                {model:'M2', serial:'S2', file:'F2', other:null}];

ArrayObject2 = [{model:'M1', serial:'S1', file:null, other:'F3'},
                {model:'M3', serial:'S3', file:null, other:'F4'}];

ExpectedResult = [{model:'M1', serial:'S1', file:'F1', other:'F3'},
                 {model:'M2', serial:'S2', file:'F2', other:null},
                 {model:'M3', serial:'S3', file:null, other:'F4'}];

I don't think jquery offers an easy method to solve your problem. 我不认为jquery提供解决问题的简便方法。 And this is my solution: 这是我的解决方案:

 var arr1 = [ { model: "M1", serial: "S1", file: "F1", other: null }, { model: "M2", serial: "S2", file: "F2", other: null } ]; var arr2 = [ { model: "M1", serial: "S1", file: null, other: "F3" }, { model: "M3", serial: "S3", file: null, other: "F4" } ]; var arr3 = arr1.concat(arr2); var result = []; arr3.forEach(function(item1, index){ var arr4 = result.filter(function(item2, index){ return item1.model === item2.model; }); if (arr4.length === 1) { for(var prop in item1){ if (item1.hasOwnProperty(prop)) { arr4[0][prop] = (item1[prop] || arr4[0][prop]); } } }else{ result.push(item1); } }); console.log(result); 

This only works for the situation when there are atmost 2 models with same model name to merge, because if there are three 'M1' and two of them have none-null 'file', then i don't know which to choose.. 这仅适用于最多有2个具有相同模型名称的模型要合并的情况,因为如果有3个“ M1”并且其中2个具有非空“ file”,那么我不知道该选择哪个。

var ExpectedResult = [];
//loop through either of the object array. Since they are of same length, it wouldn't matter which one you choose to loop though.
 for(var i = 0; i < ArrayObject2.length; i++) {
   //check to see if the array element(which are objects) have the same model property
   if(ArrayObject2[i].model === ArrayObject1[i].model){
     //if they are the same, starting switching the values of the file and other properties in either one of the array 
     //I choose ArrayObject2, it won't matter which one you decide to use

     //this chooses which is truthy between file property of ArrayObject2 at index i and file property of ArrayObject1 at index i and assigns it to the file property of ArrayObject2 at index i
     ArrayObject2[i].file = ArrayObject2[i].file || ArrayObject1[i].file;

      //this chooses which is truthy between other property of ArrayObject2 at index i and other property of ArrayObject1 at index i and assigns it to the other property of ArrayObject2 at index i
     ArrayObject2[i].other = ArrayObject2[i].other || ArrayObject1[i].other;

     //push the ArrayObject2 at position i into the ExpectedResult array
     ExpectedResult.push(ArrayObject2[i]);
   }
   //other wise; if the model property differs, just push the ArrayObject1 and ArrayObject2 at index i
   else {
     ExpectedResult.push(ArrayObject1[i]);
     ExpectedResult.push(ArrayObject2[i]);
   }
 }

 ExpectedResult;

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

相关问题 如果两个数组在Jquery中具有嵌套对象,则比较两个数组是否相等 - Compare Two arrays for equality if they have nested objects in Jquery 我需要比较两个 arrays 并让 output 成为一个数组,在里面比较对象 - I need to compare two arrays and have the output be one array, objects being compared inside 使用jQuery比较两个Javascript对象数组 - Using jQuery to compare two arrays of Javascript objects 比较对象的 arrays 并返回不在 arrays 之一中的新对象数组 - Compare arrays of objects and return new array of objects that are not in one of the arrays 比较两个对象数组 - compare two arrays of objects 比较两个对象数组 - Compare two arrays of objects 比较两个 arrays 对象(假定对象具有相同的道具,但不是值) - Compare two arrays of objects (given the objects have same props, but not values) 比较两个 arrays 的对象并根据一个对象数组排序并在不匹配时推送 - compare two arrays of objects and sort based on one array of objects and push when doesn't match 将两个Arrays与Objects进行比较,并使用不匹配的对象创建新数组 - Compare two Arrays with Objects and create new array with unmatched objects 比较两个对象数组并删除第二个对象数组中具有相同属性值的项目 - Compare two arrays of objects and remove items in the second one that have the same property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM