AdamKniec
AdamKniec

Reputation: 1717

Create array of objects based on two another arrays of objects

I went through SO to find the sulution but I failed...

I have two arrays of objects and I want to create the third one based on them and add some new keys/values.

Base Data

const oldArray = [
{id: 1, name: 'Test', age: 25, skill: 120}, 
{id: 2, name: 'Test25', age: 10, skill: 120}];

Data with some modifications

const newArray = [
{id: 1, name: 'Test2', age: 50, skill: 200},
{id: 2, name: 'Test25', age: 25, skill: 120}];

I would like to receive something like:

const expectedResultArray = [
  {id: 1, oldName: 'Test', newName: 'Test2', oldAge: 25, newAge: 50, ageDifference: 25, oldSkill: 120, newSkill: 200, skillDifference: 80 },
  {id: 2, oldName: 'Test25', newName:'Test25', oldAge: 10, newAge: 25, ageDifference: 15, oldSkill:120, newSkill: 120, skillDefference: 0}]

As You can see, the expected array is a combination of old and totally new calculations based on the two arrays. How can i achieve that ? I tried with reduce() however i got stuck trying:

var createNewArray = (...objects) => 
  Object.values(objects.reduce((a, e) => {
    a[e.id] = a[e.id] || {id: e.id};
  // struggling to proceed here 

    return a;
  }, {}))
;

console.log(createNewArr(...oldArray, ...newArray));

Upvotes: 0

Views: 53

Answers (2)

AdamKniec
AdamKniec

Reputation: 1717

Well, the solution was easier than I expected... (Facepalm)

const oldArray = [{id: 1, name: 'Test', age: 25, skill: 120},  {id: 2, name: 'Test25', age: 10, skill: 120}];

const newArray = [{id: 1, name: 'Test2', age: 50, skill: 200}, {id: 2, name: 'Test25', age: 25, skill: 120}];


const generateNewArr = () => {
 return oldArray.map((row,index) => {
   return {
     id: row.id,
     oldName: row.name,
     newName: newArray[index].name,
     oldAge: row.age,
     newAge: newArray[index].age,
     ageDifference: newArray[index].age - row.age,
     oldSkill: row.skill,
     newSkill: newArray[index].skill,
     skillDifference: newArray[index].skill - row.skill
   }
 })
}

  
console.log(generateNewArr())

Upvotes: 0

Girish Sasidharan
Girish Sasidharan

Reputation: 588

If the objects are in the same order then you can try this. Otherwise we need to find the element based on the id.

const oldArray = [
{id: 1, name: 'Test', age: 25, skill: 120}, 
{id: 2, name: 'Test25', age: 10, skill: 120}];

const newArray = [
{id: 1, name: 'Test2', age: 50, skill: 200},
{id: 2, name: 'Test25', age: 25, skill: 120}];

const Result = newArray.map((item, i) => {
  let oldConverted = renameObject(oldArray[i], 'old');
  let newConverted = renameObject(item, 'new');
  newConverted.ageDifference = newConverted.newAge - oldConverted.oldAge;
  newConverted.skillDefference = newConverted.newSkill - oldConverted.oldSkill;
  return Object.assign({}, newConverted, oldConverted);
});

function renameObject(obj, type) {
  let newObj = {};
  newObj.id = obj.id;
  Object.keys(obj).forEach((key) => {
    if (key != 'id') {
      newObj[type + capitalize(key)] = obj[key];
    }
  });
  return newObj;
}

function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
console.log(Result);

Upvotes: 1

Related Questions