在Javascript操作JSON对象,增加 删除 修改的简单实现
在Javascript操作JSON对象,增加删除修改全有的,详情见代码
<script type="text/javascript"> var jsonObj2 = { teacher: [ { name: "jordan", sex: "m", age: "40" }, { name: "bryant", sex: "m", age: "28" }, { name: "McGrady", sex: "m", age: "27" } ]}; var jsonObj2New = {}; $("#btn").click(function () { //invoke the update updateData("jordan"); var persons = jsonObj2.teacher; for (var i = 0; i < persons.length; i++) { var cur_person = persons[i]; alert(cur_person.sex); } //invoke the delete deleteData("jordan"); var persons = jsonObj2.teacher; for (var i = 0; i < persons.length; i++) { var cur_person = persons[i]; alert(cur_person.sex); } //push var temp = { name: "kenny", sex: "m", age: "25" }; jsonObj2.teacher.push(temp); var persons = jsonObj2.teacher; for (var i = 0; i < persons.length; i++) { var cur_person = persons[i]; alert(cur_person.name); } }); function updateData(name) { var persons = jsonObj2.teacher; //alert(name); for (var i = 0; i < persons.length; i++) { var cur_person = persons[i]; if (cur_person.name == name) { persons[i].sex = "f"; } } } function deleteData(name) { var persons = jsonObj2.teacher; //alert(name); for (var i = 0; i < persons.length; i++) { var cur_person = persons[i]; if (cur_person.name == name) { jsonObj2.teacher.splice(i, 1); } } } </script>
相关推荐
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23
dragonzht 2020-06-17