0

I'm trying to convert a JSON key value object to an Array, but I'm not sure how to get it in the format I need. What I have for the JSON is something similar to below:

{
  "01": "yes",
  "02": "yes",
  "03": "no"
}

but I need an Array like the one below so I can iterate through it easily:

["01:yes","02:yes","03:no"]

or is it possible to iterate through this JSON object while accessing the keys and values easily?

TechnicalTophat
  • 1,655
  • 1
  • 15
  • 37
  • Possible: Yes. Have you tried to iterate over object using `for..in` and create array from `key:value`? – Tushar May 24 '16 at 07:25

1 Answers1

1

Use Array#reduce

Object.keys() returns an array of a given object's own enumerable properties

var obj = {
  "01": "yes",
  "02": "yes",
  "03": "no"
};
var op = Object.keys(obj).reduce(function(a, b) {
  return a.concat(b + ':' + obj[b]);
}, []);
console.log(op);
Rayon
  • 36,219
  • 4
  • 49
  • 76