_javascript techniques for adding, deleting, and sorting JSON-formatted data

Source: Internet
Author: User

This example describes the method of adding, deleting and sorting JSON format data. Share to everyone for your reference, specific as follows:

JS data format and JSON data format, each has its own usefulness, personally, JSON better use a little, JS itself array and the image restrictions are more.

Examples of JS arrays:

var a = [' 1 '];
A[5] =;
A.length//Here The result is 6, that is, the middle key will be automatically filled, and the value is undefined

One, add and delete

1, one-dimensional array

Test = {}; Null JSON for like
test[' firstname '] = "tank";//Add two elements
test[' lastname '] = "Zhang";
Console.log (test); View Delete
test[' lastname '];//delete an element
Console.log (test) in JSON;

2, two-dimensional array

Test1 = [{"Name": "Tank", "Total": "M"},{"name": "Zhang", "Total": "A"},{"name": "Hao", "Total": "325"}];
Add = {"name": "May"};
Test1.push (add); Add an element
Console.log (test1);
Delete Test1[2]; Deletes an element
Console.log (test1);

Second, sorting

1, one-dimensional array

Test = ["100", "23", "325"]; Defines the array
function Sortnumber (a,b)//Definition sorting method
{return
   a-b
}
test1_sort=test.sort (sortnumber);
Console.log (Test1_sort);

2, two-dimensional array

Test1 = [{"Name": "Tank", "Total": "M"},{"name": "Zhang", "Total": "A"},{"name": "Hao", "Total": "325"}];
sort_by = function (field, reverse, primer) {//define Sort method
  var key = primer?
    function (x) {return primer (X[field])}:
    function (x) {return X[field]};
  reverse = [-1, 1][+!! Reverse];
  return function (A, b) {return
    a = key (a), B = key (b), Reverse * ((a > B)-(b > A));
   }
Test1_sort=test1.sort (sort_by (' Total ', true, parseint)); According to total, ascending row
console.log (test1_sort);
Test1_sort=test1.sort (sort_by (' name ', False, ')); According to name, reverse platoon
console.log (Test1_sort);

It's a small problem, but if you don't use it for a long time, you'll get rusty or forget it.

More readers interested in JSON-related content can view this site: A summary of JSON operation tips in JavaScript and a summary of jquery operations JSON data techniques

I hope this article will help you with your JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.