Home > Web Front-end > JS Tutorial > body text

A simple implementation of manipulating JSON objects in Javascript and adding, deleting, and modifying

韦小宝
Release: 2017-11-29 11:46:39
Original
2357 people have browsed it

In JavascriptOperation JSONObject, AddDelete Simple implementation of modification, let us take a look at JavaScript Let’s operate json. Those who are not familiar with JavaScript should take a look!

Manipulating JSON objects in Javascript, adding, deleting and modifying them all. Please see the code for details.

<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>
Copy after login

The above is the simple implementation of operating JSON objects in Javascript and adding, deleting and modifying them. Please ask for more information. Go to this siteSearch!

Related recommendations:

How to modify the pseudo-class style with JavaScript

Methods for handling JavaScript exceptions

javaScript addition, deletion, modification and query

The above is the detailed content of A simple implementation of manipulating JSON objects in Javascript and adding, deleting, and modifying. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template