Detailed explanation of jquery traversal and adding Json object code examples

伊谢尔伦
Release: 2017-07-21 10:25:56
Original
1467 people have browsed it

jquery method of dynamically traversing the properties and values of Json objects.

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

You 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.

The above is the detailed content of Detailed explanation of jquery traversal and adding Json object code examples. 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
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!