在Javascript操作JSON对象,增加 删除 修改的简单实现,让我们一起来看看JavaScript对json的操作吧,对JavaScript不熟的更应该看看了!
在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对象,增加 删除 修改的简单实现 的所有内容了,更多请到本站搜索哦!
相关推荐:
以上是在Javascript操作JSON对象,增加 删除 修改的简单实现的详细内容。更多信息请关注PHP中文网其他相关文章!