Reputation: 179
I have something like this (im obtaining it from API, so I cant change it):
{ one: [ { price: ['$10'], weight: ['1000'], color: ['red'] } ],
two: [ { price: ['$20'], weight: ['2000'], color: ['green'] } ],
three: [ { price: ['$30'], weight: ['3000'], color: ['blue'] } ] }
And I want to convert all "price", "weight" and "color" keys to its values, to look like this:
{ one: [ '$10', '1000', 'red' ],
two: [ '$20', '2000', 'green' ],
three: [ '$30', '3000', 'blue' ] }
Is there any simple way to do this?
Edit: Example fixed
Edit2: Wanted result fixed
Upvotes: 1
Views: 104
Reputation: 386560
You could map use the keys and map the wanted properties form the inner array.
var object = { one: [ { price: ['$10'], weight: ['1000'], color: ['red'] } ], two: [ { price: ['$20'], weight: ['2000'], color: ['green'] } ], three: [ { price: ['$30'], weight: ['3000'], color: ['blue'] } ] };
Object.keys(object).forEach(function (k) {
object[k] = [ 'price', 'weight', 'color'].map(function (p) {
return object[k][0][p][0];
});
});
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2