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.
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.
Use Object.values()
:
var data = {
'0': 'XXX',
'1': 'YYYY'
};
var valuesOnly = Object.values(data);
console.log(valuesOnly);
I believe you do this by
var obj = {'0': XXX, '1': YYYY};
var arr = $.map(obj, function(el) { return el });
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);
You can use plain JavaScript instead of using jQuery:
var json = {'0': "XXX", '1': "YYYY"}
Object.values(json) // ["XXX", "YYYY"]