简体   繁体   English

比较两个对象数组并合并

[英]Comparing two arrays of objects and merging

here is my use case: I have two arrays of objects that come from two observables and I have created a combineLatest method to iterate the array into one with mapped Ids: 这是我的用例:我有两个来自两个可观察对象的对象数组,并且创建了一个CombineLatest方法,以将数组迭代为具有映射ID的对象:

var result1 = [{
    question: 1,
    answerList: [{
        answer: 'Sandra',
        isDefault: 'true'
      },
      {
        answer: 'John',
        isDefault: 'false'
      }
    ]
  },
  {
    question: 2,
    answerList: [{
        answer: 'Peter',
        isDefault: 'false'
      },
      {
        answer: 'Bobby',
        isDefault: 'false'
      }
    ]
  },
  {
    question: 3,
    answerList: [{
        answer: 'Harry',
        isDefault: 'false'
      },
      {
        answer: 'Bob',
        isDefault: 'false'
      }
    ]
  }
]

var result2 = [{
    question: 1,
    answer: 'John'
  },
  {
    question: 3,
    answer: 'Bob'
  }
];

My goal is to have another array of objects containing elements like this: 我的目标是拥有另一个包含如下元素的对象数组:

var finalResult = [{
    question: 1,
    answerList: [{
        answer: 'Sandra',
        isDefault: 'false'
      },
      {
        answer: 'John',
        isDefault: 'true'
      }
    ]
  },
  {
    question: 2,
    answerList: [{
        answer: 'Peter',
        isDefault: 'false'
      },
      {
        answer: 'Bobby',
        isDefault: 'false'
      }
    ]
  },
  {
    question: 3,
    answerList: [{
        answer: 'Harry',
        isDefault: 'false'
      },
      {
        answer: 'Bob',
        isDefault: 'true'
      }
    ]
  }
]

You could use a hash table for faster check of an question with an answer is set. 您可以使用哈希表来更快地检查设置了答案的问题。 Then iterate and update the items according of the object's settings. 然后根据对象的设置迭代和更新项目。

 var result1 = [{ question: 1, answerList: [{ answer: 'Sandra', isDefault: 'true' }, { answer: 'John', isDefault: 'false' }] }, { question: 2, answerList: [{ answer: 'Peter', isDefault: 'false' }, { answer: 'Bobby', isDefault: 'false' }] }, { question: 3, answerList: [{ answer: 'Harry', isDefault: 'false' }, { answer: 'Bob', isDefault: 'false' }] }], result2 = [{ question: 1, answer: 'John' }, { question: 3, answer: 'Bob' }], object = result2.reduce((o, { question, answer }) => { (o[question] = o[question] || {})[answer] = true; return o; }, Object.create(null)); result1.forEach(({ question, answerList }) => answerList.forEach(o => o.isDefault = (question in object && o.answer in object[question]).toString() ) ); console.log(result1); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Example code: 示例代码:

let result = result1.map(item => {
    let targetItems = result2.filter( item2 => item2.question === 
                      item.question );
    targetItems.forEach(item3 => {
        item.answerList.push(item3.answer);
    });

    return item;
});

console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM