I have a set over which I am iterating in ES6. I am trying to convert it to its equivalent in ES5. My build is getting failed because of ES6. That's why I am converting it to ES5.
Here's my code in 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.
I also tried this:
for(var i = 0; i < deviceList.length(); i++){
result.push({"deviceName" : deviceList[i]});
}
Even this is not working for 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?
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. 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.
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:
deviceList.forEach(function (item) {
result.push({"deviceName": item});
});