D
D
developerrr2015-10-21 15:56:15
JavaScript
developerrr, 2015-10-21 15:56:15

How to make an array of objects from an object?

There is an object
{CHF: 9095, USD: 45966, UAH: 4324}
What to do to get a passive with objects of the form?:

[{amount:'CHF',currency: 9095},{amount:'USD',currency: 45966}, {amount:'UAH',currency: 4324}]

I try like this:
var arr= {CHF: 9095, USD: 45966, UAH: 4324},
summ = [];
_.transform(arr, function(result, n, key){
            result['amount'] = n;
            result['currency'] = key;
            summ.push(result)
        });

does not work. The output is an array with 3 identical objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hjk, 2015-10-21
@developerrr

So it won't fit?

var obj = {CHF: 9095, USD: 45966, UAH: 4324};
var arr = [];

for (var key in obj) {
  arr.push({amount: key, currency: obj[key]});
}

console.log(arr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question