Introduction to copying json objects (arrays and objects) in jQuery)

Source: Internet
Author: User
Tags javascript array

1. $. map method provided by jQuery
Copy codeThe Code is as follows:
$. Map (json, function (n) {return n ;});

This method was used to copy an array. Today, we use it to copy a record in the array. We found that the field name is lost, and then we found the second method.
2. Deep replication and light Replication
Copy codeThe Code is as follows:
// Perform shallow copy (only copy the top-level non-object elements)
Var newObject = jQuery. extend ({}, oldObject );
// Deep replication (one layer to the bottom)
Var newObject = jQuery. extend (true, {}, oldObject );

The deep replication method is used. When copying an object, it is found that the field is lost.
3. Filter Arrays
I found the method of javascript array, but there was no filtering method. Later I found jQuery provided. I tried it and it was very useful.
Copy codeThe Code is as follows:
$. Grep (jsonTmp, function (item)
{
Return item. LegendTitle = field;
}, False );

The function content can be written based on actual needs. If not, refer to jQuery's help, which is very detailed.
4. Sort arrays by the way
Copy codeThe Code is as follows:
Json2.sort (function (a, B) {return a ["requiredColumn"] ["crimeTime"]> B ["requiredColumn"] ["crimeTime"]? 1:-1 });

This is easy to use.

Related Article

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.