如何使用javascript合并两个不同的对象数组

时间:2016-08-17 10:14:43

标签: javascript node.js javascript-objects

我有两个不同的对象数组。我必须以嵌套的方式合并两个对象数组。

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

输出应该是这种格式那么如何使用循环或下划线?     OUTPUT

  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' }
                           ] 
               }]

3 个答案:

答案 0 :(得分:0)

您可以使用哈希表作为右List<CompletableFuture<Optional<ModelObject>>> collect = listOfModel.stream() .map(CompletableFuture::completedFuture) .map(f -> f.thenApplyAsync(Owner::callMethodWithReturnTypeOfOptional, executor)) .collect(Collectors.toList()); 的引用,并在必要时构建新属性questionNumber

choice

答案 1 :(得分:0)

您可以使用vanilla 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中使用assign函数或者在下划线或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" } ] } ]

答案 2 :(得分: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