0

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.

2
  • Give it a shot then Commented Apr 19, 2021 at 13:05
  • what the logic for add this user to which dummydata ?? Commented Apr 19, 2021 at 13:10

6 Answers 6

1
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 -

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

This should solve the problem:

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

3 Comments

dummyData looks to be shorter than users, use remainder to cycle dummyData index. ...dummyData[i % dummyData.length]
@pilchard that's a valid point, but JS will simply ignore spread operator when used with null or undefined
Unless the OP is looking to generate dummy data for all users with limited dummyData templates.
0

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) || {})
}], [])

Comments

0

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.