skip to Main Content

I have an array object i want to change the value of isFavorite to true only in second level (where name is pear) i tried map function but that is changing value on every level and i have written that function below can anyone look what is wrong with it?

Function

const fruitChecked = fruits.map((fruit) => ({
  ...fruit,
  attributes: fruit[1].attributes.map((attribute) => ({
    ...attribute,
    isFavorite: true,
  })),
}));

Array Example

[
  {
    name: "apple",
    attributes: [
      { type: "Granny Smith", color: "green", isFavorite: true },
      { type: "Ambrosia", color: "red", isFavorite: false },
      { type: "Persian", color: "red", isFavorite: false },
      { type: "Asian", color: "brown", isFavorite: true },
      { type: "greek", color: "red", isFavorite: false },
    ],
  },
  {
    name: "Pear",
    attributes: [
      { type: "Asian", color: "brown", isFavorite: true },
      { type: "White Pear", color: "white", isFavorite: false },
      { type: "Asian", color: "brown", isFavorite: true },
      { type: "White Pear", color: "white", isFavorite: false },
    ],
  },
];

I tried the function which i mentioned in question and also mentioned the dat with which i am trying. and i used map but that is not giving me the result“

2

Answers


  1. Maybe this is what you are after?

    const fruits=[
          {name: 'apple', 
           attributes: [
              {type: 'Granny Smith', color:'green', isFavorite: true},
              {type: 'Ambrosia', color:'red', isFavorite: false},
              {type: 'Persian', color:'red', isFavorite: false},
              {type: 'Asian', color:'brown', isFavorite: true},
              {type: 'greek', color:'red', isFavorite: false}
           ], 
     
        },
       {name: 'Pear', 
        attributes: [
          {type: 'Asian', color:'brown', isFavorite: true},
          {type: 'White Pear', color:'white', isFavorite: false},
          {type: 'Asian', color:'brown', isFavorite: true},
          {type: 'White Pear', color:'white', isFavorite: false}
        ], 
       }
      ];
     
    fruits[1].attributes.forEach(a=>a.isFavorite=true);
    
    console.log(fruits);
    Login or Signup to reply.
  2. In the map, you need to check if the fruit name is Pear before changing the attributes:

    const fruits = [
      {
        name: "apple",
        attributes: [
          { type: "Granny Smith", color: "green", isFavorite: true },
          { type: "Ambrosia", color: "red", isFavorite: false },
          { type: "Persian", color: "red", isFavorite: false },
          { type: "Asian", color: "brown", isFavorite: true },
          { type: "greek", color: "red", isFavorite: false },
        ],
      },
      {
        name: "Pear",
        attributes: [
          { type: "Asian", color: "brown", isFavorite: true },
          { type: "White Pear", color: "white", isFavorite: false },
          { type: "Asian", color: "brown", isFavorite: true },
          { type: "White Pear", color: "white", isFavorite: false },
        ],
      },
    ];
    
    const adjustedFruits = fruits.map(fruit => ({
      ...fruit,
      attributes: fruit.name === "Pear"
        ? fruit.attributes.map(att => ({
          ...att,
          isFavorite: true
        }))
        : fruit.attributes
    }));
    
    console.log(adjustedFruits);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search