简体   繁体   English

相当于 ES6 到 ES5 中的 set

[英]Equivalent of set in ES6 to ES5

I have a set over which I am iterating in ES6.我有一个我正在 ES6 中迭代的集合。 I am trying to convert it to its equivalent in ES5.我正在尝试将其转换为 ES5 中的等效项。 My build is getting failed because of ES6.由于 ES6,我的构建失败了。 That's why I am converting it to ES5.这就是我将其转换为 ES5 的原因。

Here's my code in ES6这是我在 ES6 中的代码

service.getDevices = function (date) {
        var result = [];
        var deviceList = devices[date.getTime()];

        for (let item of deviceList) { // browser compatibility: support for ECMA6
            result.push({"deviceName": item});
        }

        return result;
    }

I am getting error because of 'let'.我因为“让”而出错。 I tried using for (var item in deviceList) , it does not display the charts.我尝试使用for (var item in deviceList) ,它不显示图表。

I also tried this:我也试过这个:

for(var i = 0; i < deviceList.length(); i++){
           result.push({"deviceName" : deviceList[i]});
       }

Even this is not working for set.即使这对 set 也不起作用。 can someone help and tell me how to iterate over a set in ES5 and if that is not possible, is there any equivalent way of doing it?有人可以帮助并告诉我如何在 ES5 中迭代一个集合,如果这是不可能的,是否有任何等效的方法来做到这一点?

Why not just iterate through the data and map the result with Array#map .为什么不只是遍历数据并使用Array#map结果。

result = deviceList.map(function (item) {
    return { deviceName: item };
});

This is a basic set es5 class that I have used variations on over the years.这是一个基本的 es5 类,我多年来一直使用它的变体。

 function Set(items) { this._data = {}; this.addItems(items); } Set.prototype.addItem = function(value) { this._data[value] = true; return this; } Set.prototype.removeItem = function(value) { delete this._data[value]; return this; } Set.prototype.addItems = function(values) { for (var i = 0; i < values.length; i++) { this.addItem(values[i]); } return this; } Set.prototype.removeItems = function(values) { for (var i = 0; i < values.length; i++) { this.removeItem(values[i]); } return this; } Set.prototype.contains = function(value) { return !!this._data[value]; } Set.prototype.reset = function() { this._data = {}; return this; } Set.prototype.data = function() { return Object.keys(this._data); } Set.prototype.each = function(callback) { var data = this.data(); for (var i = 0; i < data.length; i++) { callback(data[i]); } } var set = new Set(['a', 'b', 'c']); console.log(set.addItems(['a', 'd', 'e']).removeItems(['b', 'e']).data()); console.log(set.contains('a')); console.log(set.contains('e')); set.each(console.log)

I think your problem with your second for example is just that length is a property and not a function so you shouldn't add () to the end of it.我认为你的第二个问题,你for例子仅仅是length是一个属性,而不是一个功能,所以你不应该添加()到它的结束。 A working version of this might look like this:其工作版本可能如下所示:

for(var i = 0; i < deviceList.length; i++){
  result.push({"deviceName" : deviceList[i]});
}

This assumes (as @grabantot pointed out) that deviceList is an array, however, if it's a Set then you need to use the deviceList.size property.这是假设(如@grabantot指出),其deviceList是一个数组,但是,如果它是一个Set ,那么你需要使用deviceList.size属性。

However, there is a more compatible version of your first for loop which is the forEach() function (which is available on Array and Set ), like this:但是,第一个for循环有一个更兼容的版本,它是forEach()函数(可在ArraySet 上使用),如下所示:

deviceList.forEach(function (item) {
  result.push({"deviceName": item});
});

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

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