0

I'd like to make a JSON list with different values.

Here's the code that I am trying to do.

...
// This will contain different JSON data of friendObj
var friendList = [];

// It is just default format of JSON obj.
var friendObj={
    name: "Kim",
    age: "19",
    country: "Korea"
}

// Nodejs Router
router.post("/...", function(req,res,next){
    friendList = getFriendList(10);
    ...
});

// a function to get dump data.
fun getFriendList(n){
    var list = [];

    for (var i = 0 ; i < n ; i++){
        list.push(friendObj);
        list[i].name = "Lee";

        // This will save different ages each loop.
        list[i].age = i+20;
        list[i].country = "America";
    }

    return list;
}

When I print it out, I get the friendObj with the same values with the last element. In this case, all the element would have Lee, 39, America.

But I want different values on each element.

What should I do?

4 Answers 4

1

You created the object outside the loop, so you pushed the same object to the array multiple times. Create the object inside the loop instead:

for (var i = 0; i < n; i++) {
  var friendObj = {
    name: "Kim",
    age: "19",
    country: "Korea"
  }
  list.push(friendObj);

If you're assigning new values each iteration, it would make more sense to just come up with those values and then create the object:

for (var i = 0; i < n; i++) {
  const name = // ...
  const age = // ...
  const country = // ...
  list.push({ name, age, country });
Sign up to request clarification or add additional context in comments.

Comments

1

List elements are reference the same Object (friendObj)

Check about immutable object

function getFriendList(n){
  var list = [];

  for (var i = 0 ; i < n ; i++){
      list.push(Object.assign({}, friendObj)); // Different object ref
      list[i].name = "Lee";

      // This will save different ages each loop.
      list[i].age = i+20;
      list[i].country = "America";
  }

  return list;
}

You can create immutable object with Object.assign, Object.freeze, Spread syntax, ...etc

Comments

1

This is because the same friendObj is pushed into the loop on each iteration. Each element of the array is now holding reference to the same object.

Change list.push(friendObj); inside for loop to list.push({...friendObj});. This will create a copy of the friendObj and push that copy into the array.

Note this is only a shallow copy which is fine in this case since the friendObj is not deeply nested.

Comments

0

You can try this code

I just change the little bit more technic. maybe it helps your problem solve.

  const friendList = [];

  // It is just default format of JSON obj.
  const friendObj = [
    {
      name: 'Kim',
      age: '19',
      country: 'Korea',
    },
    {
      name: 'alam',
      age: '36',
      country: 'dhaka',
    },
    {
      name: 'name3',
      age: '30',
      country: 'usa',
    },
    {
      name: 'name9',
      age: '25',
      country: 'Bangladesh',
    },
    {
      name: 'name20',
      age: '25',
      country: 'pakistan',
    },
    {
      name: 'name30',
      age: '25',
      country: 'India',
    },
  ];

  if (friendObj !== null) {
    console.log(getFriendList(4));
  }
  // a function to get dump data.
  function getFriendList(n) {
    const list = friendObj.slice(0, n);
    list.map(item => {
      item.name = 'lee';
      item.age *= 2;
      item.country = 'Bangladesh';
    });
    return list;
  }

=== Thank you ===

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.