DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 在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對象,增加 刪除 修改的簡單實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved