0

I'm new to underscore and I want to convert a json data on my controller that look like this :

{ENGLISH: "STOCK", FRENCH: "STOCK", UK: "акції"}

to this

[{key:"ENGLISH", value:"STOCK"}, {key:"FRENSH", value:"STOCK"}...]
Raja Jaganathan
  • 33,099
  • 4
  • 30
  • 33
kyserslick
  • 591
  • 1
  • 10
  • 27
  • loop over `object` and create a new `object` with `key` and `value` property then push that `object` inside a collection, that's it, refer [Converting a JS object to an array](http://stackoverflow.com/q/6857468/2435473) – Pankaj Parkar Nov 28 '16 at 09:49

2 Answers2

1
var stocks = {ENGLISH: "STOCK", FRENCH: "STOCK", UK: "акції"};

_.map(stocks,function(value, key){ 
    return {
       key:key,
       value:value
    };
});
Raja Jaganathan
  • 33,099
  • 4
  • 30
  • 33
0

You can approach this using only angular also

var input = {ENGLISH: "STOCK", FRENCH: "STOCK", UK: "акції"};
var output = [];
angular.forEach(input, function(v, k) {
  this.push({key: k, value: v});
}, output);
taguenizy
  • 2,140
  • 1
  • 8
  • 26