FrontEndDeveloper
FrontEndDeveloper

Reputation: 99

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:

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

Upvotes: 0

Views: 95

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386560

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

Upvotes: 1

Pengcheng
Pengcheng

Reputation: 323

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);

Upvotes: 0

Related Questions