Melvin
Melvin

Reputation: 33

How to merge 2 object together based on object A Key and object B value?

I have this problem which I can't find a solution to, hoping that one of you could help me with this question.

So I have 2 objects,

Object 1:

[0: {Name: 'John', DOB: '01-01-1990',ID01: 'Yes',ID02: 'No',ID03: 'Maybe'}
 1: {Name: 'Kelly', DOB: '01-01-1990',ID01: 'Yes',ID02: 'No',ID03: 'Maybe'}
 2: {Name: 'Sam', DOB: '01-01-1990', ID01: 'Yes',ID02: 'No',ID03: 'Maybe'}
 3: {Name: 'Jessie', DOB: '01-01-1990',ID01: 'Yes',ID02: 'No',ID03: 'Maybe'}]

Object 2:

[0: {QnID:'ID01', Question: 'Do you like Ice-cream'}
 1: {QnID:'ID02', Question: 'Do you like Chocolate'}
 2: {QnID:'ID03', Question: 'Do you like Cookie'}]

I would like to merge these 2 object together where the system will check the first object for the same ID and replace the ID with the question from the second table, so my final result will be like this:

[0: {Name: 'John', DOB: '01-01-1990',Do you like Ice-cream: 'Yes',Do you like Chocolate: 'No',Do you like Cookie: 'Maybe'}
1: {Name: 'Kelly', DOB: '01-01-1990',Do you like Ice-cream: 'Yes',Do you like Chocolate: 'No',Do you like Cookie: 'Maybe'}
2: {Name: 'Sam', DOB: '01-01-1990', Do you like Ice-cream: 'Yes',Do you like Chocolate: 'No',Do you like Cookie: 'Maybe'}
3: {Name: 'Jessie', DOB: '01-01-1990',Do you like Ice-cream: 'Yes',Do you like Chocolate: 'No',Do you like Cookie: 'Maybe'}]

The actual dataset is much larger.

Upvotes: 0

Views: 71

Answers (3)

PeterKA
PeterKA

Reputation: 24648

You can use a combination of map(), Object.entries(), find() and Object.fromEntries() methods as follows:

const arr1 = [
  { Name: 'John', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Kelly', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Sam', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Jessie', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
];
const arr2 = [
  { QnID: 'ID01', Question: 'Do you like Ice-cream' },
  { QnID: 'ID02', Question: 'Do you like Chocolate' },
  { QnID: 'ID03', Question: 'Do you like Cookie' },
];

const arr3 = arr1.map(({Name,DOB,...rest}) => ({
    Name,
    DOB,
    ...Object.fromEntries(
        Object.entries(rest).map(
            ([prop,val]) => [arr2.find(obj => obj.QnID === prop)['Question'],val]
        )
    )
}));

console.log( arr3 );

Upvotes: 1

Parvesh Kumar
Parvesh Kumar

Reputation: 1144

Okay first you need to fix your array structure.

const arr1 = [
  { Name: 'John', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Kelly', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Sam', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
  { Name: 'Jessie', DOB: '01-01-1990', ID01: 'Yes', ID02: 'No', ID03: 'Maybe' },
];
const arr2 = [
  { QnID: 'ID01', Question: 'Do you like Ice-cream' },
  { QnID: 'ID02', Question: 'Do you like Chocolate' },
  { QnID: 'ID03', Question: 'Do you like Cookie' },
];
const result = arr1.map((obj) =>
  Object.assign({ Name: obj.Name, DOB: obj.DOB }, ...arr2.map((obj2) => ({ [obj2.Question]: obj[obj2.QnID] })))
);
console.log(result);

Upvotes: 0

Radu Diță
Radu Diță

Reputation: 14201

I would create a map from the questions, so you have quick access to questions:

const qMap = {};

questions.forEach(q => {
 qMap[q.QnID] = q.Question;
})

and then go thru all persons.

persons.map(person => {
 const newPerson = {};

 const keys = Object.keys(person);
 keys.forEach(key => {
  if (qMap[key]) {
    newPerson[qMap[key]] = person[key];
  } else {
    newPerson[key] = person[key];
  }
 })

 return newPerson
})

Upvotes: 1

Related Questions