How to get the set value in javascript: first declare the set through the "var s=new Set();" method; then use "s.forEach(function (element, sameElement, set) {...}" The method can traverse the value of s.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascriptHow to get the value of set?
JAVASCRIPT Traverse set to get the value in set
Array in js can use subscripts, Map and Set cannot use subscripts. But Array, Map and Set both belong to iterable type. Use iterable's built-in forEach method.
Declare set (currentConfigresult is used to receive the value passed in the background after the ajax request is successful).
var s = new Set(); currentConfigresult = data.page.result; for ( var j in currentConfigresult) { set.add(currentConfigresult[j].key); }
Traverse the value of s
s.forEach(function (element, sameElement, set) { if (element == obj.key) { html += "KEY已存在"; } })
The values of element and sameElement here are the same, both are values in set. If it is a map, element corresponds to value and sameElement corresponds to key.
[Related recommendations: javascript Advanced Tutorial】
The above is the detailed content of How to get the value of set in javascript. For more information, please follow other related articles on the PHP Chinese website!