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' ]
Here is what the above code is Doing:
1. We have a personsInfo object with a company and persons property.
2. The persons property is an array of objects.
3. We are using the map method to access the name property of each object in the persons array.
4. The map method returns a new array with the names of each person.