javascript 从 _.map() 返回对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19990275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:28:39  来源:igfitidea点击:

Return object from _.map()

javascriptarraysobjectmapunderscore.js

提问by ThomasReggi

So the _.map()function in underscore doesn't return an object, but it takes them. Is there any way for it to return the exact same object it takes?

所以_.map()下划线中的函数不返回一个对象,但它接受它们。有没有办法让它返回完全相同的对象?

var _ = require("underscore");

var cars = {
    "mom": {
        "miles": "6",
        "gas": "4"
    },
    "dad": {
        "miles": "6",
        "gas": "4"
    }
}

var regurgitate_cars = _.map(cars, function(value, key){
    return value;
});

/*
[ { miles: '6', gas: '4' }, { miles: '6', gas: '4' } ]
*/

var regurgitate_cars = _.map(cars, function(value, key){
    var transfer = {};
    transfer[key] = value;
    return transfer;
});

/*
[ { mom: { miles: '6', gas: '4' } },
  { dad: { miles: '6', gas: '4' } } ]
*/

回答by EmptyArsenal

You can use _.object()to turn it back into an object.

你可以用_.object()它把它变回一个物体。

var regurgitate_cars = _.object(
    _.map(cars, function(value, key){
        return [key, value];
    })
);

As for doing that directly with _.map, you'd have to rewrite map to do it.

至于直接使用_.map,您必须重写 map 才能做到这一点。

回答by Andrew Clark

_.map()will always return an array, but you can get the behavior with _.reduce():

_.map()将始终返回一个数组,但您可以通过以下方式获得行为_.reduce()

var regurgitateCars = _.reduce(cars, function(memo, value, key) {
    memo[key] = value;
    return memo;
}, cars);

Note that this will modify and return the original object, if you wanted a copy you can provide an empty object as the third argument, which will be used as the memoargument on the first call of the anonymous function:

请注意,这将修改并返回原始对象,如果您想要一个副本,您可以提供一个空对象作为第三个参数,它将用作memo第一次调用匿名函数时的参数:

var regurgitateCars = _.reduce(cars, function(memo, value, key) {
    memo[key] = value;
    return memo;
}, {});

回答by net.uk.sweet

mapreturns an arrayso there's no way you could get it to return the original object with out writing your own. See documentation:

map返回一个,array因此您无法在不编写自己的情况下返回原始对象。请参阅文档

Produces a new array of values by mapping each value in list through a transformation function (iterator). If the native map method exists, it will be used instead. If list is a JavaScript object, iterator's arguments will be (value, key, list).

通过转换函数(迭代器)映射列表中的每个值,生成一个新的值数组。如果本机 map 方法存在,则将使用它。如果 list 是 JavaScript 对象,则迭代器的参数将为 (value, key, list)。

回答by megawac

There is no way to return a object with the current implementation of map. It's been suggestedthat the library add a .mapValues()function which would do what you like. Here's how you would add it to your code:

没有办法用 map 的当前实现返回一个对象。有人建议图书馆添加一个.mapValues()可以做你喜欢的功能。以下是将其添加到代码中的方法:

_.mixin({
    mapValues: function (input, mapper) {
        return _.reduce(input, function (obj, v, k) {
            obj[k] = mapper(v, k, input);
        }, {});
    }
});

Now you can use mapValues to return a new object:

现在您可以使用 mapValues 返回一个新对象:

var regurgitate_cars = _.mapValues(cars, function(value, key){
    return value;
});