Skip to content
Advertisement

how to add property on nested object array and edit the original array?

i have been trying doing so but in the last console log I got undefined , what is wrong??

 dishes = dishes.map((cat) => {
      cat.map((dish) => {
        console.log("cat", dish[0], "dish", dish[1]);
         dish[1].map((element) => {
          element = { ...element, category: dish[0] };
        });
      });
    });

    console.log("dishes",dishes)

Advertisement

Answer

You are forget return the map result

 dishes = dishes.map((cat) => {
  return cat.map((dish) => {
    console.log("cat", dish[0], "dish", dish[1]);
    return dish[1].map((element) => ({ ...element,
      category: dish[0]
    }))
  });
});

Still undefined check the dishes array declared properly before the map

var dishes = [something]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement