Joel Gudasalmani
Joel Gudasalmani

Reputation: 11

How to use for loop for nested arrays instead of using map

How to create a new list with all user information, but add "!" to the end of each items they own.I know to solve this with map. But i'm finding it hard as a beginner to do this with a for loop. Here is the code. I would really appreciate the help.

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];

Upvotes: 0

Views: 460

Answers (2)

pilchard
pilchard

Reputation: 12919

If you're trying to return a new array and not mutate the original you need to clone each nested object (in this case each object, and then each 'items' array).

Here using nested for...of loops in conjunction with Array#entries() to access the index and value. Each object and its nested items array is cloned using spread syntax before updating the item values and then pushing the objects to the copy array.

const array = [{ username: "john", team: "red", score: 5, items: ["ball", "book", "pen"] }, { username: "becky", team: "blue", score: 10, items: ["tape", "backpack", "pen"] }, { username: "susy", team: "red", score: 55, items: ["ball", "eraser", "pen"] }, { username: "tyson", team: "green", score: 1, items: ["book", "pen"] },];

const copy = [];

for (const object of array) {
  const objectCopy = { ...object, items: [...object.items] }
  for (const [i, string] of objectCopy.items.entries()) {
    objectCopy.items[i] = string + '!'
  }
  copy.push(objectCopy)
}

console.log(copy);

If you simply want to mutate the original array you can use nested for loops, here using destructuring to isolate the items array in the outer loop before iterating it in the inner loop.

const array = [{ username: "john", team: "red", score: 5, items: ["ball", "book", "pen"] }, { username: "becky", team: "blue", score: 10, items: ["tape", "backpack", "pen"] }, { username: "susy", team: "red", score: 55, items: ["ball", "eraser", "pen"] }, { username: "tyson", team: "green", score: 1, items: ["book", "pen"] },];

for (let i = 0; i < array.length; i++) {
  const { items } = array[i];
  for (let j = 0; j < items.length; j++) {
    items[j] += '!';
  }
}

console.log(array);

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89254

You can loop over the elements using for ... of, then loop over the items for the object with an index-based for loop to update it.

const array = [ { username: "john", team: "red", score: 5, items: ["ball", "book", "pen"] }, { username: "becky", team: "blue", score: 10, items: ["tape", "backpack", "pen"] }, { username: "susy", team: "red", score: 55, items: ["ball", "eraser", "pen"] }, { username: "tyson", team: "green", score: 1, items: ["book", "pen"] },];
for(const {items} of array){
  for(let i = 0; i < items.length; i++) items[i] += '!';
}
console.log(array);

Upvotes: 1

Related Questions