-6

I have a data which is coming like this

[
  {
    "3": "Foundation in Business",
    "4": "0",
    "5": "1103267.5",
    "6": "4277417.5",
    "7": "5168625",
    "8": "6241805",
    "9": "7383665",
    "10": "8236385",
    "11": "8645050",
    "12": "2494100",
    "13": "155555"
  }
  ]

and i want it like this way

data : [ "Foundation in Business", "0", "1103267.5", "4277417.5", "5168625", "6241805", "7383665", "8236385", "8645050", "2494100", "155555" ]

Please help me with this ...

  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](http://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) –  Apr 07 '17 at 14:24

1 Answers1

1

In ES6, map the values and get the Object values. Because the result is another array get the first element. If you would have an array of objects, you would get an array of arrays by removing the [0] at the end of the script.

const jsonObject = [{
    "3": "Foundation in Business",
    "4": "0",
    "5": "1103267.5",
    "6": "4277417.5",
    "7": "5168625",
    "8": "6241805",
    "9": "7383665",
    "10": "8236385",
    "11": "8645050",
    "12": "2494100",
    "13": "155555"
}];
const data = jsonObject.map(v => Object.values(v))[0];
console.log(data);

In cross-browser supported standard you could do it like this:

   const jsonObject = [{
    "3": "Foundation in Business",
    "4": "0",
    "5": "1103267.5",
    "6": "4277417.5",
    "7": "5168625",
    "8": "6241805",
    "9": "7383665",
    "10": "8236385",
    "11": "8645050",
    "12": "2494100",
    "13": "155555"
}];
var array = [];
for (var name in jsonObject[0]) {
  console.log(name);
  array.push(jsonObject[0][name]);
}
console.log(array);
Dez
  • 5,702
  • 8
  • 42
  • 51