0

If I had a json object that looks like this:

var json = {
  red: 1,
  blue: 2,
  green: 3
}

and would like to get two arrays, one of the keys [red, blue, green] and one for the values [1, 2, 3] without manually creating a forloop, is it possible?

I've done some research and found that Object.keys(json) gets back an array of the keys, but have not been able to find anything for values!

-edit- If an explanation can be provided as to why it has been marked as duplicate, I've quite clearly specified I'd prefer a method not to use forloops, that would be nice.

Gwen Wong
  • 357
  • 3
  • 15
  • There's no JSON in this question. What you have in that code block is an object. JSON is a string format. – Cerbrus Apr 20 '16 at 10:30
  • If you don't want loop use a library, because someone has to loop through your json. You can use http://underscorejs.org/#keys and http://underscorejs.org/#values – Mritunjay Apr 20 '16 at 10:31
  • The duplicate question includes answers that at least explicitly do not do looping; as Mritunjay pointed out, A) something has to, and B) libraries exist that can do it (the answer also indicates that ecma2017 will have this as a native action) – Foon Apr 20 '16 at 11:44

0 Answers0