0

I have tried this to assign id to objects of an array and the id is its index no. But forEach loop give me 0 for all index I don't know why but please help me complete this....

let allBloggerFeed = [{title, image, link, m_price, m_off, m_isAvail, cont}];
    allBloggerFeed.forEach((item, i) => {
     item.id = "product-" + i + 1;
})

allBloggerFeedis returning different Arrays not only one it's coming from API of blogger but when I tried to add ids in it like

[{id:{title, image, link, m_price, m_off, m_isAvail, cont}}

Then it's not working the forEach giving 0 for all

Please solve this.....

1
  • let allBloggerFeed = [{title, image, link, m_price, m_off, m_isAvail, cont}]; this is not valid. Commented Feb 6, 2021 at 14:42

3 Answers 3

2

Try with a map rather than forEach. The map will return a new array with the modified values.

  const allBloggerFeed = [{title, image, link, m_price, m_off, m_isAvail, cont}];
  const feedWithIds = allBloggerFeed.map((item, i) => {
    return {
      ...item,
      id: "product-" + i + 1, 
    } 
  })
Sign up to request clarification or add additional context in comments.

5 Comments

allBloggerFeed.map((item, i) => (item.id = 'product-' + (i + 1)) && item); ... is already sufficient enough; there is no need for creating a new object by assigning the old one together with a new property to it. But of cause in the end it's also a matter of taste. I just had to mention the former.
There are many ways to skin the programming cat :)
One potential pitfall to adding properties directly do the object is that your mutating the object. If you later need access to allBloggerFeed[0] it will have the id on it. Not likely to be an issue in this case but data mutation bugs do happen and can be really hard to spot and solve.
I'm entirely aware of that, and mutation is what the OP was doing.
Btw, your answer is perfectly fine, my approach would have been the same. And my comment was not criticizing anything just commenting in its best means. Ah, and the upvote is coming.
0

Your allBloggerFeed variable is an array with 1 element. Notice: Your array has one element which is an object with properties (title, image...).

So when you invoke forEach() on that array - it will only run the loop once. And the only index you have is 1.

If you want all your objects (item, images...) to be in an array - just declare it differently:

let allBloggerFeed = [title, image, link, m_price, m_off, m_isAvail, cont];
  • without the {} brackets.

1 Comment

It's not one element its its more than one which is coming from blogger api i just want it to give me output like this [{"product-1": {title, image, id, link, m_price, m_off, m_isAvail, cont}}] and so on with api product-1 is its id
0

To be honest I don't understand what you're exactly trying to do. The code you are using, will do something like this:

let array = [{a:'a',b:'b',c:'c'}, {d:'d',e:'e',f:'f'}, {g:'g',h:'h',i:'i'}]

array.forEach((item, i) => {
     item.id = "product-" + (i + 1);
})

console.log(array)

And give you something like this:

[
  { a: 'a', b: 'b', c: 'c', id: 'product-1' },
  { d: 'd', e: 'e', f: 'f', id: 'product-2' },
  { g: 'g', h: 'h', i: 'i', id: 'product-3' }
]

Is this what are you trying to do?

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.