2016-08-17 71 views
-4

我有兩個不同的對象數組。我必須以嵌套的方式合併兩個對象數組。如何使用javascript合併兩個不同的對象數組

var array1=[{ PersonalID: '11', qusetionNumber: '1', value: 'Something' }, 
       { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, 
       { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, 
       { PersonalID: '14', qusetionNumber: '4', value: 'null' }]; 

var array2=[{ qusetionNumber: '2', chID: '111', cValue: 'red' }, 
      { qusetionNumber: '2', chID: '112', cValue: 'green'}, 
      { qusetionNumber: '2', chID: '113', cValue: 'blue' }, 
      {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, 
      {qusetionNumber: '4', choiceID: '115', cValue: 'red'}]; 

輸出應該是這樣的格式那麼如何使用循環或下劃線? 輸出

personalID:[{ 
      personalID:11, 
      qusetionNumber:1, 
      value: 'Something' 
      }, 
       { 
      personalID:12, 
      qusetionNumber:2, 
      value: 'whatever', 
      choice:[       
       { qusetionNumber: '2', chID: '111', cValue: 'red' }, 
         { qusetionNumber: '2', chID: '112', cValue: 'green'}, 
         { qusetionNumber: '2', chID: '113', cValue: 'blue' } 
       ] 
      }, 
      { 
      personalID:13, 
      qusetionNumber:3, 
      value: 'anything', 
      choice:[       
       { qusetionNumber: '3', chID: '114', cValue: 'yellow' } 
          ] 
       }, 
       { 
      personalID:14, 
      qusetionNumber:4, 
      value: 'null', 
      choice:[       
       { qusetionNumber: '4', chID: '115', cValue: 'red' } 
          ] 
       }] 
+0

我嘗試過使用forloop,但我沒有得到正確的結果 –

+0

'arrayBy'在array2上,然後'map'在array1上 – Gant

+0

可以請你分享一下你的代碼嗎? –

回答

0

你可以使用一個哈希表作爲參考權questionNumber,如有必要建立一個新的屬性choice

var array1 = [{ PersonalID: '11', questionNumber: '1', value: 'Something' }, { PersonalID: '12', questionNumber: '2', value: 'whatever' }, { PersonalID: '13', questionNumber: '3', value: 'anything' }, { PersonalID: '14', questionNumber: '4', value: 'null' }], 
 
    array2 = [{ questionNumber: '2', chID: '111', cValue: 'red' }, { questionNumber: '2', chID: '112', cValue: 'green' }, { questionNumber: '2', chID: '113', cValue: 'blue' }, { questionNumber: '3', choiceID: '114', cValue: 'yellow' }, { questionNumber: '4', choiceID: '115', cValue: 'red' }], 
 
    hash = Object.create(null); 
 

 
array1.forEach(function (a) { 
 
    hash[a.questionNumber] = a; 
 
}); 
 

 
array2.forEach(function (a) { 
 
    if (hash[a.questionNumber]) { 
 
     hash[a.questionNumber].choice = hash[a.questionNumber].choice || []; 
 
     hash[a.questionNumber].choice.push(a); 
 
    } 
 
}); 
 

 
console.log(array1);

+0

hash [a.questionNumber] .choice = hash [a.questionNumber] .choice || [];^ TypeError:無法讀取未定義的屬性'選擇'它給出了上述錯誤 –

+0

如果'questionNumber'引用存在,您可以添加一個檢查。 –

+0

我已經解決了錯誤,但我無法得到所有的選擇..mean爲personalID:'12'我得到所有的選擇,但其他人我沒有得到 –

0

您可以只用香草JS以下方式做到這一點。

樣品在現代ES6 +

var array3 = array1.map(question => { 
    let newQuestion = Object.assign({},question); // In order not to mutate the items array1 
    newQuestion.choice = array2.filter(choice => choice.qusetionNumber === question.qusetionNumber); 
    return newQuestion; 
}); 

舊的語法:

var array3 = array1.map(function(question) { 
    var newQuestion = Object.assign({},question); // In order not to mutate the items array1 
    newQuestion.choice = array2.filter(function(choice){ return choice.qusetionNumber === question.qusetionNumber}); 
    return newQuestion; 
}); 

如果你不能在Object.assign是可用計數,您可以使用分配功能lodash或下劃線或jQuery的擴展

var array1 = [{ 
 
    PersonalID: '11', 
 
    qusetionNumber: '1', 
 
    value: 'Something' 
 
}, { 
 
    PersonalID: '12', 
 
    qusetionNumber: '2', 
 
    value: 'whatever' 
 
}, { 
 
    PersonalID: '13', 
 
    qusetionNumber: '3', 
 
    value: 'anything' 
 
}, { 
 
    PersonalID: '14', 
 
    qusetionNumber: '4', 
 
    value: 'null' 
 
}]; 
 

 
var array2 = [{ 
 
    qusetionNumber: '2', 
 
    chID: '111', 
 
    cValue: 'red' 
 
}, { 
 
    qusetionNumber: '2', 
 
    chID: '112', 
 
    cValue: 'green' 
 
}, { 
 
    qusetionNumber: '2', 
 
    chID: '113', 
 
    cValue: 'blue' 
 
}, { 
 
    qusetionNumber: '3', 
 
    choiceID: '114', 
 
    cValue: 'yellow' 
 
}, { 
 
    qusetionNumber: '4', 
 
    choiceID: '115', 
 
    cValue: 'red' 
 
}]; 
 

 
var array3 = array1.map(function(question) { 
 
    var newQuestion = Object.assign({}, question); // In order not to mutate the items array1 
 
    newQuestion.choice = array2.filter(function(choice) { 
 
    return choice.qusetionNumber === question.qusetionNumber 
 
    }); 
 
    return newQuestion; 
 
}); 
 

 
console.log(JSON.stringify(array3, null, 4));

結果:

[ { "PersonalID": "11", "qusetionNumber": "1", "value": "Something", "choice": [] }, { "PersonalID": "12", "qusetionNumber": "2", "value": "whatever", "choice": [ { "qusetionNumber": "2", "chID": "111", "cValue": "red" }, { "qusetionNumber": "2", "chID": "112", "cValue": "green" }, { "qusetionNumber": "2", "chID": "113", "cValue": "blue" } ] }, { "PersonalID": "13", "qusetionNumber": "3", "value": "anything", "choice": [ { "qusetionNumber": "3", "choiceID": "114", "cValue": "yellow" } ] }, { "PersonalID": "14", "qusetionNumber": "4", "value": "null", "choice": [ { "qusetionNumber": "4", "choiceID": "115", "cValue": "red" } ] } ]

+0

除PersonID:'12'我不會爲別人選擇。 –

0

這是我的解決方案,也許這將是有益的

var array1=[{ PersonalID: '11', qusetionNumber: '1', value: 'Something' }, 
       { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, 
       { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, 
       { PersonalID: '14', qusetionNumber: '4', value: 'null' }]; 
var array2=[{ qusetionNumber: '2', chID: '111', cValue: 'red' }, 
      { qusetionNumber: '2', chID: '112', cValue: 'green'}, 
      { qusetionNumber: '2', chID: '113', cValue: 'blue' }, 
      {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, 
      {qusetionNumber: '4', choiceID: '115', cValue: 'red'}]; 

var i = -1; 
array1.forEach(function(a){ 
var arrayWithQuestions = []; 
    array2.forEach(function(y){ 
    if(a.qusetionNumber == y.qusetionNumber){ 
     i++; 
     arrayWithQuestions.push(array2[i]);   
    } 
    }) 
    if(arrayWithQuestions.length!=0){ 
    a.choice = arrayWithQuestions; 
    } 
    console.log(arrayWithQuestions); 
}) 
console.log('how array1 looks like'); 
console.log(array1); 

結果:

[ 
    { 
     "PersonalID":"11", 
     "qusetionNumber":"1", 
     "value":"Something" 
    }, 
    { 
     "PersonalID":"12", 
     "qusetionNumber":"2", 
     "value":"whatever", 
     "choice":[ 
      {"qusetionNumber":"2","chID":"111","cValue": "red"}, 
      {"qusetionNumber":"2", "chID":"112","cValue":"green"}, 
      {"qusetionNumber":"2", "chID":"113", "cValue":"blue"} 
      ] 
    }, 
    { 
     "PersonalID":"13", 
     "qusetionNumber":"3", 
     "value":"anything", 
     "choice": [ 
      {"qusetionNumber":"3", "choiceID":"114", "cValue":"yellow"} 
     ] 
    }, 
    { 
    "PersonalID":"14", 
    "qusetionNumber":"4", 
    "value":"null", 
    "choice": [ 
     {"qusetionNumber":"4", "choiceID":"115", "cValue":"red"} 
     ] 
    } 
] 

see jsfiddle

相關問題