Home  >  Article  >  Web Front-end  >  How to get the value of set in javascript

How to get the value of set in javascript

藏色散人
藏色散人Original
2021-06-30 10:32:377948browse

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.

How to get the value of set in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn