I want to convert my json to array
var data = {1:{id: '2014-924', name: 'abc'},2:{id: '2014-925', name: 'xyz'}};
var result = ['2014-924','2014-925'];`outout`
I want to convert my json to array
var data = {1:{id: '2014-924', name: 'abc'},2:{id: '2014-925', name: 'xyz'}};
var result = ['2014-924','2014-925'];`outout`
2
Answers
Your data should be in the following format
The you can deserialise the json using JSON.parse();
And to create the result array:
The value you want in your result array is present in the values for
id
in your object. You can loop through the values of the object to get the ‘id’.Use Object.values() to get an array of values of your object. Then loop through the array to collect the id.
Here is an example using Array.map