Home >Web Front-end >JS Tutorial >Implementation of traversing the properties of a json object and dynamically adding properties
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] === 'string'){ alert("person中"+item+"的值="+person[item]); }else if(typeof person[item] === 'function'){ person[item](1,1);//js 的function的参数可以动态的改变 } } //添加属性 person.isMe = 'kaobian'; // 这种是属性名字正常的 //当属性名字不正常时,像下面这种,必须用这种形式的, person['isMe.kaobian'] = 'hello kaobian'; //上面的也可以用下面的形式 for(var item in person){ if(typeof person[item] === 'string'){ alert("person中"+item+"的值="+person[item]); }else if(typeof person[item] === 'function'){ 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!