iNcizzle
iNcizzle

Reputation: 3

Change Object Attribute Variable On Loop

var i;
for (i = 0; i < account.feed.data.length; i++) { 
  const rendernew = Object.assign(render ,{account_post_VARIABLE I HERE: `${account.feed.data[i].id}`});
}

I would like to make it so once it loops is changes the attribute so it adds a new line instead of re writing the value of the attribute. Any ideas, Thanks!!! :)

Expected Output:

{
  account_post_0: "1234"
  account_post_1: "5678"
  account_post_2: "9012"
}

Upvotes: 0

Views: 46

Answers (1)

apple apple
apple apple

Reputation: 10591

It should be as simple as assign each attribute in a loop.

let account={feed:{data:[1,2,3,4,5,6,7,8,9,10].map(x=>({id:x}))}}

let obj={};
for (let i = 0; i < account.feed.data.length; i++) { 
  obj[`account_post_${i}`] = `${account.feed.data[i].id}`
}

console.log(obj)

Upvotes: 1

Related Questions