简体   繁体   English

如何使用数组和map函数构建一个javascript对象?

[英]how to build a javascript object using an array and the map function?

I've got an array of channels that I want to transform into a single object (channelSettings) with a true / false property for each channel . 我有一个通道数组 ,我想将其转换为单个对象 (channelSettings), 每个通道都有一个true / false 属性

I've got it working using the below code but it seems verbose. 我已经使用下面的代码工作,但它似乎很冗长。 Is there are way to do it without the "temp" var? 没有“temp”变量,有没有办法做到这一点? If I can get ride of that, then I could get ride of the self executing function as well. 如果我能够驾驭它,那么我也可以驾驭自动执行功能。

var channels = ["TV", "Billboard", "Spot TV"];


var channelSettings = function() {
    var temp = {};

    channels.map(function(itm, i, a) {
        var channel = itm.toLowerCase().replace(" ", "");
        temp[channel] = false;
    });

    return temp;
}();

I guess I'm trying to get the map function to return an object with properties instead of an array. 我想我正在尝试让map函数返回一个具有属性而不是数组的对象。 Is this possible? 这可能吗? Is it mis-guided? 这是误导吗? Suggestions? 建议?

This is what I'm hoping it looks like in the end: 这是我希望它最终看起来像:

var channels = ["TV", "Billboard", "Spot TV"];

var channelSettings = channels.map(function(itm, i, a) {
        var channel = itm.toLowerCase().replace(" ", "");
        return ????;
});

Use a .reduce() function instead. 请改用.reduce()函数。

var channelSettings = channels.reduce(function(obj, itm) {
        var channel = itm.toLowerCase().replace(" ", "");
        obj[channel] = false;

        return obj;
}, {});

DEMO: http://jsfiddle.net/MjW9T/ 演示: http //jsfiddle.net/MjW9T/


The first parameter references the previously returned item, except for the first iteration, where it references either the first item in the Array, or the seeded item, which we provided as an empty object. 第一个参数引用先前返回的项,第一次迭代除外,它引用数组中的第一项或者我们作为空对象提供的种子项。

The second parameter references the current value in the Array. 第二个参数引用Array中的当前值。 As long as we always return obj , the first parameter will always be that object, as will the final return value. 只要我们总是返回obj ,第一个参数将始终是该对象,最终返回值也是如此。

The map function takes an array, and returns an array. map函数接受一个数组,并返回一个数组。 Nothing else. 没有其他的。 But you can use reduce : 但你可以使用reduce

var settings = ["TV", "Billboard", "Spot TV"].reduce(function(obj, item) {
   obj[item.toLowerCase().replace(" ", "")] = false; // probably too concise
   return obj // yay, we can skip a semi-colon here :-P
}, {});

Well, "am not i am" beat me to it, but anyway: 好吧,“我不是我”打败了它,但无论如何:
map not only returns arrays, but also only returns arrays of the same length as the original. map不仅返回数组,还返回与原始数组长度相同的数组。 It's meant for transforming one array's values 1:1 into a new array. 它用于将一个数组的值1:1转换为新数组。 reduce is meant to "reduce an array to a single value". reduce意味着“将数组减少到单个值”。 Hence its use here. 因此它在这里使用。

If you use a straight for loop or the forEach method to add properties to an object, you do need to declare that object. 如果使用直接for循环或forEach方法向对象添加属性,则需要声明该对象。 So, no, you can't do without temp in your code (unless you use reduce instead of a loop). 所以,不,你不能在代码中没有temp (除非你使用reduce而不是循环)。

More information on MDN: 有关MDN的更多信息:

  1. map: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map 地图: https//developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
  2. reduce: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce reduce: https//developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce

hmm.. looks like wrapping it in a function like this would do it. 嗯..看起来像在这样的函数中包装它会这样做。

function toObject(arr) {
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    if (arr[i] !== undefined) rv[arr[i]] = true;
  return rv;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM