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.