相当于 ES6 到 ES5 中的集合
Equivalent of set in ES6 to ES5
我有一个正在 ES6 中迭代的集合。我正在尝试将其转换为 ES5 中的等效项。由于 ES6,我的构建失败了。这就是我将其转换为 ES5 的原因。
这是我在 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;
}
由于 'let',我收到错误消息。我尝试使用 for (var item in deviceList)
,它不显示图表。
我也试过这个:
for(var i = 0; i < deviceList.length(); i++){
result.push({"deviceName" : deviceList[i]});
}
即使这对 set 也不起作用。有人可以帮助我告诉我如何在 ES5 中迭代一个集合吗?如果这不可能,是否有任何等效的方法?
为什么不直接遍历数据并将结果映射到 Array#map
。
result = deviceList.map(function (item) {
return { deviceName: item };
});
我认为你的第二个 for
示例的问题只是 length
是一个 属性 而不是函数,所以你不应该将 ()
添加到它的结束。它的工作版本可能如下所示:
for(var i = 0; i < deviceList.length; i++){
result.push({"deviceName" : deviceList[i]});
}
这假定(正如 @grabantot 指出的那样)deviceList
是一个数组,但是,如果它是 Set
那么您需要使用 deviceList.size
属性.
但是,第一个 for
循环有一个更兼容的版本,即 forEach()
函数(在 Array and Set 上可用),如下所示:
deviceList.forEach(function (item) {
result.push({"deviceName": item});
});
这是一个基本的 es5 class 集,多年来我一直在使用它的变体。
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)
我有一个正在 ES6 中迭代的集合。我正在尝试将其转换为 ES5 中的等效项。由于 ES6,我的构建失败了。这就是我将其转换为 ES5 的原因。
这是我在 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;
}
由于 'let',我收到错误消息。我尝试使用 for (var item in deviceList)
,它不显示图表。
我也试过这个:
for(var i = 0; i < deviceList.length(); i++){
result.push({"deviceName" : deviceList[i]});
}
即使这对 set 也不起作用。有人可以帮助我告诉我如何在 ES5 中迭代一个集合吗?如果这不可能,是否有任何等效的方法?
为什么不直接遍历数据并将结果映射到 Array#map
。
result = deviceList.map(function (item) {
return { deviceName: item };
});
我认为你的第二个 for
示例的问题只是 length
是一个 属性 而不是函数,所以你不应该将 ()
添加到它的结束。它的工作版本可能如下所示:
for(var i = 0; i < deviceList.length; i++){
result.push({"deviceName" : deviceList[i]});
}
这假定(正如 @grabantot 指出的那样)deviceList
是一个数组,但是,如果它是 Set
那么您需要使用 deviceList.size
属性.
但是,第一个 for
循环有一个更兼容的版本,即 forEach()
函数(在 Array and Set 上可用),如下所示:
deviceList.forEach(function (item) {
result.push({"deviceName": item});
});
这是一个基本的 es5 class 集,多年来我一直在使用它的变体。
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)