Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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++;
})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading