使用下划线将对象转换为数组

d3h*_*ead 5 javascript arrays object underscore.js

我正在尝试使用Underscore将javascript对象转换为数组,但是在理解Underscore时遇到一些问题。我想隐瞒这个:

{ key1: value1, key2: value2},{key1: value1, key2: value2}
Run Code Online (Sandbox Code Playgroud)

变成这个:

[value1, value2],[value1, value2]
Run Code Online (Sandbox Code Playgroud)

the*_*eye 5

您可以使用_.map_.values喜欢这个

var data = [{ key1: 1, key2: 2 }, { key1: 3, key2: 4 }];
console.log(_.map(data, _.values));
# [ [ 1, 2 ], [ 3, 4 ] ]
Run Code Online (Sandbox Code Playgroud)

如果您喜欢通用JavaScript版本,则可以

console.log(data.map(function(currentObject) {
    return Object.keys(currentObject).map(function(currentKey) {
        return currentObject[currentKey];
    })
}));
# [ [ 1, 2 ], [ 3, 4 ] ]
Run Code Online (Sandbox Code Playgroud)