0

Code:

let fruits = [{
    name: 'apple',
    attributes: [{
        type: 'Granny Smith',
        color: 'green',
        isFavorite: true
      },
      {
        type: 'Ambrosia',
        color: 'red',
        isFavorite: true
      }
    ],
    isFavorite: true
  },
  {
    name: 'Pear',
    attributes: [{
        type: 'Asian',
        color: 'brown',
        isFavorite: true
      },
      {
        type: 'White Pear',
        color: 'white',
        isFavorite: false
      }
    ],
    isFavorite: true
  },
]

const fruitChecked = fruits.map(fruit => {
  return { ...fruit,
    isFavorite: true
  }
})

const fruitAttributesChecked = fruitChecked.map(fruit => {


  (fruit.attributes).map(attr => {
    return { ...attr,
      isFavorite: true
    }

  })
})

console.log(fruits)
console.log(fruitChecked)
console.log(fruitAttributesChecked)

I am trying to change ALL the isFavorite value to true within each object and the isFavorite value within attributes to true.

However I'm getting something similar to this for [undefined,undefined] for fruitAttributesChecked

The outcome I desire is below for fruitAttributesChecked

fruitAttributesChecked =
[
   {name: 'apple', 
    attributes: [
          {type: 'Granny Smith', color:'green', isFavorite: true},
          {type: 'Ambrosia', color:'red', isFavorite: true}
    ], 
     isFavorite: true
   },
   {name: 'Pear', 
    attributes: [
          {type: 'Asian', color:'brown', isFavorite: true},
          {type: 'White Pear', color:'white', isFavorite: false}
    ], 
     isFavorite: true
   },
]

What am I missing here?

3
  • 2
    You're not returning the value of (fruit.attributes).map Commented Aug 18, 2021 at 23:03
  • Sorry new to this. That makes sense, I have to return ``` (fruit.attributes).map(attr => { return { ...attr, isFavorite: true } ``` Commented Aug 18, 2021 at 23:04
  • @Robson thats a typo. the desired should have all true Commented Aug 18, 2021 at 23:05

2 Answers 2

4

You can use a nested map() to return a modified attributes array in the object.

let fruits = [{
    name: 'apple',
    attributes: [{
        type: 'Granny Smith',
        color: 'green',
        isFavorite: true
      },
      {
        type: 'Ambrosia',
        color: 'red',
        isFavorite: true
      }
    ],
    isFavorite: true
  },
  {
    name: 'Pear',
    attributes: [{
        type: 'Asian',
        color: 'brown',
        isFavorite: true
      },
      {
        type: 'White Pear',
        color: 'white',
        isFavorite: false
      }
    ],
    isFavorite: true
  },
]

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

console.log(fruitChecked);

Sign up to request clarification or add additional context in comments.

Comments

1
const mappedFruits = fruits.map(fruit => {
    return {
        ...fruit,
        isFavorite: true,
        attributes: attributes.map(attribute => {
            return {
                ...attribute,
                isFavorite: true,
            }
        })
    }
})

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.