富贵资源网 Design By www.hznty.com
在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>
以上这篇在Javascript操作JSON对象,增加 删除 修改的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...