Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array of objects into array of properties [duplicate]

Is there a simple way, using filter or parse or something else to convert an array like the following :

var someJsonArray = [   {id: 0, name: "name", property: "value", otherproperties: "othervalues"},   {id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"},   {id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"} ]; 

into a simple array filled with one attribute of the objects contained in the previous array like this :

[0, 1, 2] 
like image 560
Ellone Avatar asked Dec 16 '15 10:12

Ellone


People also ask

How do you convert an array of objects to an array?

To convert an object to an array you use one of three methods: Object.keys() , Object.values() , and Object.entries() . Note that the Object.keys() method has been available since ECMAScript 2015 or ES6, and the Object.values() and Object.entries() have been available since ECMAScript 2017.

How to convert array object to array in to object in JavaScript?

const arr = [{id:1,name:"aa"},{id:2,name:"bb"},{id:3,name:"cc"}]; We are required to write a JavaScript function that takes in one such array and returns an object of the object where the key of each object should be the id property.

How to convert array of objects to one object?

The quickest way to convert an array of objects to a single object with all key-value pairs is by using the Object. assign() method along with spread operator syntax ( ... ). The Object.

How to convert an array to a Set react?

To convert an Array to a Set in JavaScript, pass the array as a parameter to the Set() constructor - new Set(arr) . The Set constructor is used to create Set objects, that store unique values. Copied!

How to convert an array of objects to an array?

Given an array of objects and the task is to convert the object values to an array with the help of JavaScript. There are two approaches that are discussed below: Approach 1: We can use the map () method and return the values of each object which makes the array.

How do I create an array of objects with different properties?

To start, specify the path to the parent object or collection. Then, specify the property subpath within the parent where the array is located. Next, specify a property name for each child object's key and a new property name for the properties within each child object. Finally, specify the path where the new object property will be written to.

How to get the value of an object in an array?

Approach 1: We can use the map () method and return the values of each object which makes the array. Approach 2: The Object.keys () method is used to get the keys of object and then those keys are used to get the values of the objects from the array.

How to iterate over an object and convert it to array?

The solution to this is quite simple and straightforward, we will use the Object.keys () method to iterate over the object simultaneously converting it into an array like this.


2 Answers

Use .map() function:

finalArray = someJsonArray.map(function (obj) {   return obj.id; }); 

Snippet

var someJsonArray = [    {id: 0, name: "name", property: "value", therproperties: "othervalues"},    {id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"},    {id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"}  ];  var finalArray = someJsonArray.map(function (obj) {    return obj.id;  });  console.log(finalArray);

The above snippet is changed to make it work.

like image 178
Praveen Kumar Purushothaman Avatar answered Sep 22 '22 18:09

Praveen Kumar Purushothaman


A more minimal example (ES6 onwards):

someJsonArray.map(({id}) => id) 
like image 43
coatesap Avatar answered Sep 21 '22 18:09

coatesap