1

This part of code only shows the first and second person in the database "Ban" status.

console.log("Banned0",res.data.data[0].banned); //display the first person banned status
console.log("Banned1",res.data.data[1].banned);  //display the second person banned status

enter image description here

I would like to console.log all 5 person banned status without repeatedly using console.log.

3
  • 1
    res.data.map(item => console.log(item.data.banned)) should do it. :) Commented Jun 27, 2022 at 6:00
  • That's not what map is meant to be used for. Try forEach or for...of. Commented Jun 27, 2022 at 6:06
  • Ok then. res.data.forEach - Happy? Commented Jun 27, 2022 at 6:12

4 Answers 4

4

One possible solution, using Array.prototype.forEach:

res.data.data.forEach((data, idx) => console.log('Banned' + idx, data.banned));
Sign up to request clarification or add additional context in comments.

Comments

3

If you find an array, you must looping the data, u can use this function

array.map(data => console.log(data));
or
array.forEach((data) => console.log(data));

Read this articles: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration?retiredLocale=id

1 Comment

as Andy said in another comment ... don't use map
3

You can use any kind of for loop to iterate over the data.

My suggestion is to either use a for of loop:

for (const dataItem of res.data.data) {
    console.log("Banned", dataItem.banned);
}

or a forEach loop on the array, which would also easily get you the index:

res.data.data.forEach((dataItem, index) => {
    console.log(`Banned ${index}`, dataItem.banned);
});

Comments

1

The key is to finding a way to iterate over an array of objects each with a banned status. There are a few ways to approach this.

You could use the old-school for statement:

const res={data:{data:[{banned:true,name:"Bob"},{banned:true,name:"Zoe"},{banned:true,name:"Gary"},{banned:false,name:"Fred"}]}};

for (let i = 0; i < res.data.data.length; i++) {
  const { name, banned } = res.data.data[i];
  console.log(`Is ${name} banned? ${banned}`);
}

You could use forEach. (Note: the limitation of forEach is you can't return a value early if, for example, your loop was in a function, or break or continue).

const res={data:{data:[{banned:true,name:"Bob"},{banned:true,name:"Zoe"},{banned:true,name:"Gary"},{banned:false,name:"Fred"}]}};

res.data.data.forEach(user => {
  const { name, banned } = user;
  console.log(`Is ${name} banned? ${banned}`);
});

Finally here's for...of:

const res={data:{data:[{banned:true,name:"Bob"},{banned:true,name:"Zoe"},{banned:true,name:"Gary"},{banned:false,name:"Fred"}]}};

for (const user of res.data.data) {
  const { name, banned } = user;
  console.log(`Is ${name} banned? ${banned}`);
}

Additional documentation

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.