Answers for ".reduce, object to array, index by property"

0

.reduce, object to array, index by property

// const peopleArray = [
//     {
//         "id": "aron",
//         "hobby": "games"
//     },
//     {
//         "id": "scott",
//         "hobby": "ninjitsu"
//     }
// ];

const indexedPeople = peopleArray.reduce( 
	(ac, p) => ({...ac, [p.id]: p }), {} )
    
// {
// 	"aron": {
// 	        "id": "aron",
// 	        "hobby": "games"

// 	},
// 	"scott": {
// 		"id": "scott",
// 	        "hobby": "ninjitsu"
// 	}
// }
Posted by: Guest on October-17-2021

Code answers related to ".reduce, object to array, index by property"

Code answers related to "Javascript"

Browse Popular Code Answers by Language