2017-10-26 4 views
0

誰でも以下のシナリオで役立つでしょうか? モデルとシリアルに基づいて比較する2つの配列オブジェクトがあり、1つの結果のみを出力する必要があります。 以下のサンプルを参照してください。ありがとう。jQueryは2つの配列オブジェクトを比較して最終的な配列オブジェクトを1つ持っています

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'}]; 
+1

[JavaScriptで2つの配列をマージしてアイテムを重複させる方法](https://stackoverflow.com)の可能な複製/ questions/1584370/how-to-merge-two-arrays-in-javascript-and-duplicate-items) –

+0

両方の入力配列に同じ 'model'と' serial'を持つオブジェクトがありますが、 'null'以外の' file'または 'other'のオブジェクトがあるとどうなりますか? (ちなみに、有効なオブジェクトリテラル構文を表示するために質問を編集することは嫌です。すなわち、 '='ではなく ':'を使用します) – nnnnnn

+0

入力配列は常にサンプルを好きにします。各入力配列はモデルとシリアルのみを持ちます。モデルとシリアルはそのオブジェクトを一意にします。ありがとう。 – Donna

答えて

0

私はjqueryがあなたの問題を簡単に解決する方法を提供しているとは思わない。そして、これは私のソリューションです:

var arr1 = [ 
 
\t { model: "M1", serial: "S1", file: "F1", other: null }, 
 
\t { model: "M2", serial: "S2", file: "F2", other: null } 
 
]; 
 

 
var arr2 = [ 
 
\t { model: "M1", serial: "S1", file: null, other: "F3" }, 
 
\t { model: "M3", serial: "S3", file: null, other: "F4" } 
 
]; 
 

 
var arr3 = arr1.concat(arr2); 
 
var result = []; 
 

 
arr3.forEach(function(item1, index){ 
 
\t var arr4 = result.filter(function(item2, index){ 
 
\t \t return item1.model === item2.model; 
 
\t }); 
 
\t if (arr4.length === 1) { 
 
\t \t for(var prop in item1){ 
 
\t \t \t if (item1.hasOwnProperty(prop)) { 
 
\t \t \t \t arr4[0][prop] = (item1[prop] || arr4[0][prop]); 
 
\t \t \t } 
 
\t \t } 
 
\t }else{ 
 
\t \t result.push(item1); 
 
\t } 
 
}); 
 

 
console.log(result);

3「M1」は存在する場合ので、マージする同じモデル名の[最大2つのモデルは、ある状況でしか動作し、それらの2 nullでない 'file'がある場合は、どちらを選択するかわからない。

0
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; 
関連する問題