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]
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.
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.
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.
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!
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.
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.
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.
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.
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.
A more minimal example (ES6 onwards):
someJsonArray.map(({id}) => id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With