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.

1
  • 3
    shouldn't the json be {'0': XXX, '1': YYYY} Commented Aug 6, 2017 at 14:25

4 Answers 4

5

Use Object.values():

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

var valuesOnly = Object.values(data);

console.log(valuesOnly);

Sign up to request clarification or add additional context in comments.

Comments

1

I believe you do this by

var obj = {'0': XXX, '1': YYYY};
var arr = $.map(obj, function(el) { return el });

Comments

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);

Comments

0

You can use plain JavaScript instead of using jQuery:

var json = {'0': "XXX", '1': "YYYY"}
Object.values(json) // ["XXX", "YYYY"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.