如何映射对象?

时间:2015-05-14 16:01:26

标签: javascript

例如,我们可以像这样映射一个数组:

IMGOINGTOBENUMBERS = ["1", "5", "19"];

IM_HAPPY = IMGOINGTOBENUMBERS.map(function(value){
        return +value;
});

console.log(IM_HAPPY);

关于jsfiddle:http://jsfiddle.net/87npme9e/1/

但是,常规JS对象呢?我们知道.map仅适用于数组。

对象:

IMGOINGTOBENUMBERS = {
    a: "1",
    b: "2"
}

NOT_HAPPY = IMGOINGTOBENUMBERS.map(function(value){
        return +value;
});

console.log(NOT_HAPPY);

关于jsfiddle:http://jsfiddle.net/uyzqtpLd/2/

1 个答案:

答案 0 :(得分:1)

IMGOINGTOBENUMBERS = {
    a: "1",
    b: "2"
}



NOT_HAPPY =  Object.keys(IMGOINGTOBENUMBERS).map(function (key) {return IMGOINGTOBENUMBERS[key]});

console.log(NOT_HAPPY);