javascript怎么取set的值

文章2023-01-0792 人已阅来源:网络

javascript取set值的方法:首先通过“var s=new Set();”方法声明set;然后通过“s.forEach(function (element, sameElement, set) {...}”方法遍历s的值即可。

javascript怎么取set的值

本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript怎么取set的值?

JAVASCRIPT 遍历set获取set中的值

js中Array可以使用下标,Map和Set不能使用下标。但是Array,Map,Set都属于iterable类型。使用iterable内置的forEach方法。

声明set(currentConfigresult是用来接收ajax请求成功后后台传递的值)。

var s = new Set();
currentConfigresult = data.page.result;
for ( var j in currentConfigresult) {
    set.add(currentConfigresult[j].key);
}

遍历s的值

s.forEach(function (element, sameElement, set) {  
    if (element == obj.key) {
        html += "KEY已存在";
                
    }
})

这里的element和sameElement的值相同,都是set里面的值。如果是map,element对应value和sameElement对应key。

【相关推荐:javascript高级教程】

以上就是javascript怎么取set的值的详细内容!