Home > Web Front-end > JS Tutorial > body text

How to get the value of set in javascript

藏色散人
Release: 2023-01-07 11:41:24
Original
7941 people have browsed it

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);
}
Copy after login

Traverse the value of s

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template