简体   繁体   中英

Create a JavaScript Array using value from JavaScript Objects

I'm newbie to JavaScript, I have a JavaScript object

[{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]

I want to create an array like

[31, 23, 31, 49, 110, 115, 104]

I referred some links, but they are showing how to create object from array, not array from object. How to get the key value in JSON object?

Creating javascript object from array values

Creating a JavaScript Object from two arrays

I tried like this:

for (var i = 0; i < chartDataG4S.length; i++) {
        var data = chartDataG4S[i]['G4S'];
        console.log(data);
      }

But it wont create an array. I'm stuck with this, any kind of help is welcome, thanks in advance.

You can simply use .map() :

 let data = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]; let result = data.map(({ G4S }) => Number(G4S)); console.log(result);

Use .map to extract the G4S property in each object, and cast to a number, creating a new array in the process:

 const input = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]; const output = input.map(({ G4S }) => Number(G4S)); console.log(output);

Or, the for loop version, if you prefer:

 const input = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]; const output = []; for (let i = 0; i < input.length; i++) { output.push(Number(input[i].G4S)); } console.log(output);

 let chartDataG4S = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]; let result = chartDataG4S.map(o => Number(o.G4S)); console.log(result);

The function map() which originates from functional programming and is inherent in JavaScript's arrays, facilitates accessing the value of each object's G4S property, namely a numeric string. The function Number() takes each string value and converts it to a number. That return value is then added to a new result array.

While Array object's map() is convenient to use, note you may also achieve the same results with explicit iteration as follows:

 let chartDataG4S = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}] let result = []; for (var i=0; i in chartDataG4S; i++) { result.push(Number(chartDataG4S[i].G4S)); } console.log(result);

Try this

var newArray = [];  // New array declaration to be used to push the values.
var objArray = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}];  // Your array of objects

objArray.forEach(function(elem){
    newArray.push(elem.G4S);
});

You can do this in simple steps , get arrays of all values of object using map and merge all into single one here is how.

 let data = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S:"49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}]; //step 1 let arr = data.map(da => Object.values(da)); // irrespective of keys //step 2 let result = arr.reduce((acc, val) => acc.concat(val), []); console.log(result); // in one liner. console.log(data.map(da => Object.values(da)).reduce((acc, val) => acc.concat(val), []))

This is basically an array and you can do like this.

let arr = [{G4S: "31"},{G4S: "23"},{G4S: "31"},{G4S: "49"},{G4S: "110"},{G4S: "115"},{G4S: "104"}];
let arr1 =[];
for(let i=0; i<are.length; i++)
    arr1.push(are[i].G4S);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM