Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

javascript - Equivalent of set in ES6 to ES5

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not just iterate through the data and map the result with Array#map.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...