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

jquery method to dynamically traverse the properties and values ​​of a Json object

高洛峰
Release: 2017-01-14 11:13:36
Original
1052 people have browsed it

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 attributes 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 jquery method of dynamically traversing the properties and values ​​of Json objects is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more jquery methods to dynamically traverse the properties and values ​​of Json objects, please pay attention to 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