1

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

10
  • Do you mean you want to convert to: [ '$10', 1000, 'red' ]? [ { $10, 1000, red } ] is not valid Commented Mar 7, 2017 at 18:02
  • 2
    The format you are trying to attain isn't valid data. Commented Mar 7, 2017 at 18:03
  • please add [Object]. Commented Mar 7, 2017 at 18:03
  • Ok, I will edit post soon. I will add these objects into example. // Done Commented Mar 7, 2017 at 18:05
  • 1
    @Chamov { '$10', '1000', 'red' } still isn't valid. Commented Mar 7, 2017 at 18:10

1 Answer 1

2

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; }

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, and greetings from D-Dorf Reisholz :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.