How do I create an object from array of objects using javascript?

user3867250
const arr = [{name:"abc", age:10},{name:"xyz", age:20},{name:"asd", age:12}];

Need output as:

{abc: {age:10}, xyz: {age:20}, asd: {age: 12}}
Octavian Mărculescu

You could use reduce to iterate through all the array items and build a new object with the desired format:

const arr = [{name:"abc", age:10},{name:"xyz", age:20},{name:"asd", age:12}];
const result = arr.reduce((acc, currentValue) => {
  // grab the name in a separate variable and keep the rest of the object in another using object destructuring
  const { name, ...rest } = currentValue;
  // use the name as a key in the result and assign the rest as the value
  acc[name] = rest;
  return acc;
}, {});
console.log(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert array of Objects into one Object in JavaScript?

How do I remove objects from a JavaScript associative array?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert array of objects to object in javascript?

How do I create a new object from an existing object in Javascript?

How do I create a react list component from an array of objects?

How do I create a third object from two objects using the key values

Create nested object from array of objects in JavaScript

How do I extract an object(array) from an array of objects?

How do I create an array of partial objects from another array?

How to create Object with nested Objects from an Array

Create one object from a array of objects in Javascript

How do I convert array of Objects to one Object in JavaScript?

How do I filter an array object inside of a array of objects Javascript?

How do I create a javascript array of lists of objects?

how do I create an array of objects with array in the object from an object with the same key in javascript

Javascript create array of unique objects from object

In javascript, how do I create a nested array or object from json data?

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I modify an object value in a cloned array of objects in Javascript

Create array of objects from an object using javascipt?

How to manipulate limited objects from an array of object using typescript/javascript

How to create array of objects from object of objects?

How to create array of objects from array: javascript

How to create a new objects of array by using object values in JavaScript

Create array of multiple objects with different structure from array of objects with duplicate keys using Object.assign() in JavaScript

How to create an array of object inside another array of object from an array of Objects in javascript

How to push a new object to an array of objects using a from input in JavaScript?

How do I create new arrays of objects with objects from an existing array, based on values in the objects?