Home > Web Front-end > JS Tutorial > Detailed explanation of the steps to dynamically traverse Json object properties and values ​​using jquery

Detailed explanation of the steps to dynamically traverse Json object properties and values ​​using jquery

php中世界最好的语言
Release: 2018-04-25 15:22:59
Original
2107 people have browsed it

This time I will bring you a detailed explanation of the steps to use jquery to dynamically traverse Json object properties and values. What are the precautions for using jquery to dynamically traverse Json object properties and values? The following are Let’s take a look at practical cases.

1. Traverse the attributes of the json object

//定义json对象
 var person= {
 name: 'zhangsan',
 pass: '123',
 fn: function(){
   alert(this.name+"的密码="+this.pass);
 }
 }
 //遍历person属性包括方法,如果不想显示出方法,可用typeof(person[item])== "function"来判断
 for(var item in person){
 alert("person中"+item+"的值="+person[item]);
 }
Copy after login

2. Dynamically add attributes to the json object

Need to use the person object in 1

var copyPerson={}  //创建copyPerson对象,将person中的属性包括方法copy给该对象
 for(var item in person){
 copyPerson[item]= person[item];  //这样循环就可以将person中的属性包括方法copy到copyPerson中了
 }
 
 for(var item in copyPerson){
 alert("copyPerson中"+item+"的值="+person[item]);
 }
Copy after login

Note: Using Ext.apply(copyPerson, person) you can also copy all attributes including methods in person to copyPerson

3. Traverse the properties of ordinary js objects

//定义一个普通的js类,包含方法
 var p= function (){
 this.name= '李四';
 this.pass= '456';
 this.fn= function(){
  alert(this.name+"的密码="+this.pass);
 }
 
 }
 var pp= new p();  //生成一个p类的对象 pp
 
 for(var item in pp){
 
 //遍历pp对象中的属性,只显示出 非函数的 属性,注意不能 遍历 p这个类
 if(typeof(pp[item])== "function")
  continue;
 alert("p对象中"+item+"的属性="+pp[item]);
 }
Copy after login

Ordinary js objects can also be copied. The copy method has the same idea as 2. Dynamically adding attributes to the json object.

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

Recommended reading:

Detailed explanation of the use of AJAX request queue

Detailed explanation of the steps for jQuery ajax to read the page using the get() function

The above is the detailed content of Detailed explanation of the steps to dynamically traverse Json object properties and values ​​using jquery. 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