Niishaw
Niishaw

Reputation: 141

Combining Data from one object array to another object array

I'm looking to combine an array returned from my local Database with dummy data to be sent to the frontend.

I have the two arrays of objects, Users come directly from my mongo database and before I send the result to the frontend, I would like to add the dummy data to the user object array. Now the Users list is a lot larger than my dummy data and thus I would like to loop through my user data and append the dummyData to every user object and once I reach the end of my dummyData, it should just start from the top again.

let users = [
      { name: "John", lastName: "Henry" },
      { name: "Peter", lastName: "Pumpkin" },
      {name: "John", lastName: "Snow"},
      {name: "Jack", lastName: "Stevens"}

    ];
let dummyData = [
      {
        callTime: "Call Now",
        userType: "End User(Agent)",
        callStatus: "Called",
        orderStatus: "Order Placed(1)",
        consultant: "Bennie Hennie",
        company: "Some Company"
      },
      {
        callTime: "Call Now",
        userType: "End User(James Bond)",
        callStatus: "Pending Call",
        orderStatus: "No Order Placed",
        consultant: "Sally Sue",
        company: "Super Sonic & Co"
      },
      {
        callTime: "Call Now",
        userType: "End User(Peter Griffin))",
        callStatus: "Called",
        orderStatus: "Order Placed(1)",
        consultant: "Jenny Oldstone",
        company: "Witcher School"
      }
    ];

The desired end result should be:

users[
       {name: "John", 
        lastName: "Henry",
        callTime: "Call Now",
        userType: "End User(Agent)",
        callStatus: "Called",
        orderStatus: "Order Placed(1)",
        consultant: "Bennie Hennie",
        company: "Some Company" 
     }]

I think I can achieve this via a forloop, but I've never looped through objects before.

Upvotes: 0

Views: 54

Answers (6)

Brandon McConnell
Brandon McConnell

Reputation: 6119

A simple reduce function should do the trick here. In fact, I did some testing, and it appears you don't even need to do any extra checks for whether dummyData[i] exists. An index being undefined doesn't throw an error and doesn't break the functionality at all. Those indexes merely get skipped, which would be the desired functionality anyway if there were ever a mismatch in the number of elements, I would presume.

If you are wanting to overwrite the users array of objects with the new array, this should do exactly what you are looking for:

users = users.reduce((a,c,i) => [...a, {...c, ...dummyData[i]}], []);

Here it is in action:

let users = [{ name: "John", lastName: "Henry" }, { name: "Peter", lastName: "Pumpkin" }, { name: "John", lastName: "Snow" }, { name: "Jack", lastName: "Stevens" }];
let dummyData = [{ callTime: "Call Now", userType: "End User(Agent)", callStatus: "Called", orderStatus: "Order Placed(1)", consultant: "Bennie Hennie", company: "Some Company" }, { callTime: "Call Now", userType: "End User(James Bond)", callStatus: "Pending Call", orderStatus: "No Order Placed", consultant: "Sally Sue", company: "Super Sonic & Co" }, { callTime: "Call Now", userType: "End User(Peter Griffin))", callStatus: "Called", orderStatus: "Order Placed(1)", consultant: "Jenny Oldstone", company: "Witcher School" }];

users = users.reduce((a,c,i) => [...a, {...c, ...dummyData[i]}], []);

console.log(users);

Upvotes: 0

chrwahl
chrwahl

Reputation: 13070

You can use Object.assign().

var user0 = { name: "John", lastName: "Henry" };

var data0 = {
  callTime: "Call Now",
  userType: "End User(Agent)",
  callStatus: "Called",
  orderStatus: "Order Placed(1)",
  consultant: "Bennie Hennie",
  company: "Some Company"
};

var sum0 =  Object.assign(user0, data0);

console.log(sum0);

Full example:

let users = [
      { name: "John", lastName: "Henry" },
      { name: "Peter", lastName: "Pumpkin" },
      {name: "John", lastName: "Snow"},
      {name: "Jack", lastName: "Stevens"}

    ];
let dummyData = [
      {
        callTime: "Call Now",
        userType: "End User(Agent)",
        callStatus: "Called",
        orderStatus: "Order Placed(1)",
        consultant: "Bennie Hennie",
        company: "Some Company"
      },
      {
        callTime: "Call Now",
        userType: "End User(James Bond)",
        callStatus: "Pending Call",
        orderStatus: "No Order Placed",
        consultant: "Sally Sue",
        company: "Super Sonic & Co"
      },
      {
        callTime: "Call Now",
        userType: "End User(Peter Griffin))",
        callStatus: "Called",
        orderStatus: "Order Placed(1)",
        consultant: "Jenny Oldstone",
        company: "Witcher School"
      }
    ];
    
users2 = users.map((user, i) => {
  Object.assign(user, dummyData[i]);
});

console.log(users);

Upvotes: 1

Patryk Cieszkowski
Patryk Cieszkowski

Reputation: 751

Array.prototype.reduce seems like a more readable approach.

const output = users.reduce((acc, user, index) => [...acc, { ...user, ...data[index] } ], [])

Although it's not necessary, you might want to verify there absolutely is a corresponding data index to each user object:

const output = users.reduce((acc, user, index) => data[index]
  ? [...acc, { ...user, ...data[index] } ]
  : [...acc, user ], [])

I would also encourage you, to provide some sort of identifier to each data/user object, that way you wouldn't have to rely on their order.

const output = users.reduce((acc, user) => [...acc, {
  ...user,
  ...(data.find(dUser => dUser.id === user.id) || {})
}], [])

Upvotes: 0

Anil
Anil

Reputation: 716

let k = 0;
users.forEach((item,i)=> {
 k = k === dummyData.length ? 0 : k;
 users[i] =  {...users[i],...dummyData[k]};
 k++;
});
console.log(users)

Try this -

Upvotes: 1

Konrad Uciechowski
Konrad Uciechowski

Reputation: 486

This should solve the problem:

for (var i = 0; i < users.length; i++) { 
    users[i] = {...users[i], ...dummyData[i]}
}

Upvotes: 0

chrwahl
chrwahl

Reputation: 13070

You can use a for-loop and treat the object as an array.

var user0 = { name: "John", lastName: "Henry" };

var data0 = {
  callTime: "Call Now",
  userType: "End User(Agent)",
  callStatus: "Called",
  orderStatus: "Order Placed(1)",
  consultant: "Bennie Hennie",
  company: "Some Company"
};

for(d in data0){
  user0[d] = data0[d];
}

console.log(user0);

Upvotes: -1

Related Questions