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

Introduction to the copying method of json objects in jQuery (arrays and objects)_jquery

WBOY
Release: 2016-05-16 17:32:35
Original
935 people have browsed it
1. The $.map method that comes with jQuery
Copy the code The code is as follows:

$.map(json, function (n) { return n; });

This method was originally used to copy an array. Today I use it to copy a certain item in the array. Record, found that the field name was missing, and later discovered the second method.
2. Deep copy and shallow copy
Copy code The code is as follows:

// Shallow copy (only copy top-level non-object elements)
var newObject = jQuery.extend({}, oldObject);
// Deep copy (copy layer by layer until Bottom layer)
var newObject = jQuery.extend(true, {}, oldObject);

uses the deep copy method, and the phenomenon of missing fields when copying objects is no longer found.
3. Array filtering
I looked for the JavaScript array method, but there is no filtering method. Later I found that jQuery provides it. I tried it and it worked very well.
Copy code The code is as follows:

$.grep(jsonTmp, function (item)
{
return item.LegendTitle == field;
}, false);

Just write the content of the function according to the actual needs. If not, refer to the help of jQuery, there Very detailed.
4. By the way, the sorting of the array
Copy the code The code is as follows:

json2.sort(function (a, b) { return a["requiredColumn"]["crimeTime"] > b["requiredColumn"]["crimeTime"] ? 1 : -1 });

This is also more convenient to use
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!