3

I have an JSON file:

['0': XXX, '1': YYYY]

I would like to extract this into an array with only the value via jquery.

['XXX', 'YYYY']

What is the best way to do this?

.makeArray doesn't seem to work.

H C
  • 1,138
  • 4
  • 21
  • 39

4 Answers4

5

Use Object.values():

var data = {
  '0': 'XXX',
  '1': 'YYYY'
};

var valuesOnly = Object.values(data);

console.log(valuesOnly);
ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
1

I believe you do this by

var obj = {'0': XXX, '1': YYYY};
var arr = $.map(obj, function(el) { return el });
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

You have to use JSON.parse this way:

function parsing(jsonStr){

     return JSON.parse(jsonStr);
 }
var jsonStr = "['0': XXX, '1': YYYY]";
var arr = parsing(jsonStr);
console.log(arr);
asmmahmud
  • 4,844
  • 2
  • 40
  • 47
0

You can use plain JavaScript instead of using jQuery:

var json = {'0': "XXX", '1': "YYYY"}
Object.values(json) // ["XXX", "YYYY"]
José Quinto Zamora
  • 2,070
  • 12
  • 15