Ganesh Aher
Ganesh Aher

Reputation: 1128

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.

Upvotes: 2

Views: 146

Answers (6)

user10587375
user10587375

Reputation:

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);

Upvotes: 0

slevy1
slevy1

Reputation: 3832

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);

Upvotes: 1

Manikant Gautam
Manikant Gautam

Reputation: 3581

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), []))

Upvotes: 0

Ghost Rider
Ghost Rider

Reputation: 161

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);
});

Upvotes: 0

Mohammad Usman
Mohammad Usman

Reputation: 39322

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);

Upvotes: 4

CertainPerformance
CertainPerformance

Reputation: 370679

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);

Upvotes: 2

Related Questions