Home  >  Article  >  Web Front-end  >  Implementation of traversing the properties of a json object and dynamically adding properties

Implementation of traversing the properties of a json object and dynamically adding properties

高洛峰
高洛峰Original
2017-01-04 09:43:301627browse

Yesterday, because of a requirement of the company, I studied the traversal and dynamic modification of the properties of the json object:

var person= {  
 name: 'zhangsan',  
 pass: '123' ,  
 'sni.ni' : 'sss',  
 hello:function (){  
   for(var i=0;i<arguments.length;i++){  
       //在不知参数个数情况下可通过for循环遍历        
       // arguments这个是js 默认提供  
      alert("arr["+i+"]="+arguments[i]);  
   }    
 }  
 }  
    
//遍历属性  
 for(var item in person){  
  if(typeof person[item] === &#39;string&#39;){  
   alert("person中"+item+"的值="+person[item]);  
  }else if(typeof person[item] === &#39;function&#39;){  
    person[item](1,1);//js 的function的参数可以动态的改变  
  }  
 }  
//添加属性  
  
 person.isMe = &#39;kaobian&#39;; // 这种是属性名字正常的  
//当属性名字不正常时,像下面这种,必须用这种形式的,  
 person[&#39;isMe.kaobian&#39;] = &#39;hello kaobian&#39;; //上面的也可以用下面的形式  
  
 for(var item in person){  
  if(typeof person[item] === &#39;string&#39;){  
   alert("person中"+item+"的值="+person[item]);  
  }else if(typeof person[item] === &#39;function&#39;){  
  
    person[item](1,1);  
  }  
 }

The above article is about the implementation of traversing the properties of the json object and dynamically adding properties, which is shared by the editor. I’ve given you all the content, I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more related articles on traversing the properties of json objects and dynamically adding properties, please pay attention to the PHP Chinese website!


Statement:
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