Home > Enterprise >  How can I not use a counter in a foreach to reach an object
How can I not use a counter in a foreach to reach an object

Time:10-05

I would like to restore some of the content of restoredCommentObject, which is an object.

I would like to do so for every element into the object, but do I have to put a counter to do so or is there any way to do it in a better way (without using an useless counter)

let counter = 0;
const createRestoredComment = (restoredCommentObject) => {

    restoredCommentObject.forEach(restoredShit => { 
        let restoredText = restoredCommentObject[counter].text;
        let restoredId = restoredCommentObject[counter].id;
        counter  ;
})

CodePudding user response:

When doing the forEach, the convention is as follows:

const createRestoredComment = (restoredCommentObject) => {
    restoredCommentObject.forEach(restoredShit => { 
        let restoredText = restoredShit.text;
        let restoredId = restoredShit.id;
})

No need to use the index var.

  • Related