0
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"
}
14
  • Can you explain further, with an example input and desired output? Commented Aug 9, 2018 at 3:21
  • Computed Property Names. {["account_post_"+i]: `${account.feed.data[i].id}`} Commented Aug 9, 2018 at 3:23
  • Ok thanks I will try Commented Aug 9, 2018 at 3:25
  • @iNcizzle Had a mistake, edited the code in the previous comment. Commented Aug 9, 2018 at 3:27
  • 1
    may I ask why you don't use array? Commented Aug 9, 2018 at 3:29

1 Answer 1

1

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)

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

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.