Pratik
Pratik

Reputation: 1541

Create array from values of different objects using Underscore.js

I have following kind of data

    "val1": [0.31, 0.3069, 0.3038],
    "val2": ["2015-01-14", "2015-06-14", "2016-01-14"],
    "val3": [0.1, 0.11, 0.11]

I want to create different array which will be as follows

    [0.31, "2015-01-14", 0.1],
    [0.3069, "2015-06-14", 0.11],
    [0.3038, "2016-01-14", 0.11]

How can I create this kind of array using underscore.js ? Can anyone please tell me how to do this.

Thanks

Upvotes: 0

Views: 86

Answers (1)

thefourtheye
thefourtheye

Reputation: 239473

You can use _.values and _.zip like this

var obj = {
    "val1": [0.31, 0.3069, 0.3038],
    "val2": ["2015-01-14", "2015-06-14", "2016-01-14"],
    "val3": [0.1, 0.11, 0.11]
};

console.log(_.zip.apply(_, _.values(obj)));

Output

[ [ 0.31, '2015-01-14', 0.1 ],
  [ 0.3069, '2015-06-14', 0.11 ],
  [ 0.3038, '2016-01-14', 0.11 ] ]

Upvotes: 1

Related Questions