Home Web Front-end JS Tutorial Detailed explanation of jquery traversal and adding Json object code examples

Detailed explanation of jquery traversal and adding Json object code examples

Jul 21, 2017 am 10:25 AM
javascript jquery json

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization tips for converting PHP arrays to JSON

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

How do annotations in the Jackson library control JSON serialization and deserialization?

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

Quick tips for converting PHP arrays to JSON

Conquer the pinnacle of Java JSON processing: parse and create complex data Conquer the pinnacle of Java JSON processing: parse and create complex data Mar 09, 2024 am 09:13 AM

Conquer the pinnacle of Java JSON processing: parse and create complex data

PHP Tutorial: How to Convert JSON Unicode to Chinese Characters PHP Tutorial: How to Convert JSON Unicode to Chinese Characters Mar 05, 2024 pm 06:36 PM

PHP Tutorial: How to Convert JSON Unicode to Chinese Characters

Problems and solutions for converting PHP arrays to JSON Problems and solutions for converting PHP arrays to JSON May 01, 2024 pm 01:30 PM

Problems and solutions for converting PHP arrays to JSON

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute?

See all articles