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

Detailed explanation of the steps to obtain key and value in js loop map

php中世界最好的语言
Release: 2018-05-30 15:05:06
Original
11815 people have browsed it

This time I will bring you a detailed explanation of the steps to obtain key and value with js loop map. What are the precautions for js loop map to obtain key and value? The following is a practical case, let's take a look.

The following method 1 and method 2 have been tested and run by the editor of Script House

Method 1: json format definition





无标题文档

Copy after login

Code 2,

var obj = { 
 "a": 1, 
 "b": 2, 
 "c": 3 
}; 
 
for (var prop in obj) { 
 if (obj.hasOwnProperty(prop)) {  
 // or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety... 
  alert("prop: " + prop + " value: " + obj[prop]) 
 } 
}
Copy after login

The following are the supplements of other netizens. You can refer to them

javascriptLoopTraverse the arrayOutput key value

javascript loops through the array to output key value
Using the $.each method will definitely not work, so use the following method

markers = [];
    markers[2]=3;
    markers[3]=7;
    for(var key in markers){
      console.log( key )
      console.log( markers[key] )
    }
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting things, please pay attention to php Other related articles on the Chinese website!

Recommended reading:

How to deal with async/await wasted performance problem

jQuery implementation of fuzzy query practical case analysis

The above is the detailed content of Detailed explanation of the steps to obtain key and value in js loop map. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!