Answers for "How to use JavaScript map() method to access nested objects?"

1

js map nested objects

const items = [
  { id: 1, name: 'Nike Air Max 97', inStock: true },
  { id: 2, name: 'Adidas Continental', inStock: true },
  { id: 3, name: 'Adidas Supercourt', inStock: true },
  { id: 4, name: 'Nike Jordan Dunks', inStock: true }
]
// ? Create a new array "itemsCopy" from it using map
// update the Nike Jordans Dunks in the copyArray to 
// have an "inStock" value of false. 
// Make sure using console.log that you have not affected 
// the original items array.
const itemsCopy = items.map(item => {
  if (item.id === 4) {
    return { ...item, inStock: false }
  }
  return { ...item }
})
console.log('og', items)
console.log('new', itemsCopy)
Posted by: Guest on April-27-2021
0

How to use JavaScript map() method to access nested objects?

//access value by using map
var personsInfo = {
    company: 'sp-coder',
    persons: [{
        id: 1,
        name: 'Adil'
    }, {
        id: 2,
        name: 'Arif'
    }]
};
const getNames = personsInfo.persons.map((data) => data.name);
console.log(getNames);
//Expected Output is [ 'Adil', 'Arif' ]
Posted by: Guest on September-03-2021

Code answers related to "How to use JavaScript map() method to access nested objects?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language