[英]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.