The _javascript techniques of associative array and common array in JS

Source: Internet
Author: User
var privarr = [];
  privarr[' staprojquerygrid '] = [{
    btn_id: ' But_add ',
    roles: [' 2001 ', ' the ' of the '] ']
  
  console.log (Privarr, PRIVARR.STAPROJQUERYGRID[0].BTN_ID)

The first line is to define an array Priarr, and the second line is to add a property staprojquerygird to the array, which is an array. Print result is But_add

var Unprivarr = [];//creates an empty array and assigns it to Unprivarr

unprivarr[' 1000 ']=[];//inserts an empty array into the empty array unprivarr, equivalent to unprivarr[1000]=[]

unprivarr[' 1000 ' [' AAA ']={' but_check1 ': 1, ' But_check2 ': 1};

Add a property named AAA to the empty array above (because JS is all objects, so the array is also an object, you can add properties and methods), the property value is a new object (that is, {' But_check1 ': 1, ' But_check2 ': 1}).

There is a big difference between this and the above. First the second line if [] inside is a number, then the array unpriarr the 1001th element is also an empty array (temporarily called X), the first 1000 elements are undifined,

If it's a variable, it's an array unpriarr element.

The third line is to add a property to x AAA, the attribute value is an associative array {' But_check1 ': 1, ' But_check2 ': 1}

You can use x[' but_check1 ' to get the corresponding value

And I want to do associative arrays add attributes to an object?

var Unprivarr = [];//creates an empty array and assigns it to Unprivarr

unprivarr[' 1000 ']=[];//inserts an empty array into the empty array unprivarr, equivalent to unprivarr[1000]=[]

unprivarr[' 1000 ' [' AAA ']={' but_check1 ': 1, ' But_check2 ': 1};

Console.log (unprivarr[1000].aaa[' But_check1 ')

This can output the result 1 correctly, or it can output the result with unprivarr[1000].aaa.but_check1, but if quoting is wrong

If the third line to {} inside the quotation mark removed, fourth will be an error, at this time can only use Unprivarr[1000].aaa.but_check1

Above this JS in the associative array and the general array of details is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.