2

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.

Eddie
  • 26,593
  • 6
  • 36
  • 58
Ganesh Aher
  • 1,118
  • 3
  • 21
  • 51

6 Answers6

4

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);
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
2

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);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Great, thanks buddy it's working fine. Just quick question, why we use const rather than var? – Ganesh Aher Nov 01 '18 at 06:02
  • 1
    `var` has problems that can trip you up - for example, it has unintuitive function scope, rather than block scope, and gets hoisted. `const` does not have either of those issues, and as another plus, when you see `const`, you *know* that the following variable will never be reassigned, which makes reading and understanding code at a glance easier. Best to use `const` whenever you can, use `let` when you can't (which doesn't have `var`'s problems, but is reasignable), and never use `var`. (Transpile to ES5 during your build process to support ancient browsers) – CertainPerformance Nov 01 '18 at 06:06
1

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);
slevy1
  • 3,797
  • 2
  • 27
  • 33
0

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);
});
Ghost Rider
  • 161
  • 1
  • 1
  • 10
0

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), []))
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
0

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);
DHARMENDRA SINGH
  • 607
  • 5
  • 21