Detailed Explanation of Associative Array and Ordinary Array in js

  • 2021-07-06 09:38:22
  • OfStack


var privArr = [];
  privArr['staProjQueryGrid'] = [{
    btn_id : 'but_add',
    roles : ['2001','2005']
  }]
  
  console.log(privArr,privArr.staProjQueryGrid[0].btn_id)

Line 1 defines an array priArr, and line 2 adds an attribute staProjQueryGird to the array, whose value is an array. The print result is but_add


var unPrivArr = [];// Create 1 An empty array and assign it to unPrivArr

unPrivArr['1000']=[];// To empty array unPrivArr Internal insertion 1 An empty array, equivalent to  unPrivArr[1000]=[]

unPrivArr['1000']['aaa']={'but_check1':1,'but_check2':1};

// Add to the empty array inserted above 1 A person named aaa Property of because js Li 1 All right   Image, so the array is also an object, and attributes and methods can be added), and the attribute value is 1 New objects (that is, {'but_check1':1,'but_check2':1} ). 

This is quite different from the above. First of all, if there are numbers in [] in line 2, it means that the 1001st element of the array unPriArr is also an empty array (temporarily called x), and the first 1000 elements are undifined.

If it is a variable, it is an element of the array unPriArr

The third line is to add an attribute aaa to x, and the attribute value is an associative array {'but_check1': 1, 'but_check2': 1}

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

I wonder if associative arrays add attributes to objects.


var unPrivArr = [];// Create 1 An empty array and assign it to unPrivArr

unPrivArr['1000']=[];// To empty array unPrivArr Internal insertion 1 An empty array, equivalent to  unPrivArr[1000]=[]

unPrivArr['1000']['aaa']={'but_check1':1,'but_check2':1};

console.log(unPrivArr[1000].aaa['but_check1'])

This will output 1 correctly, or unPrivArr [1000]. aaa.but_check1, but it is wrong to quote

If the quotation marks in {} are removed from line 3, number 4 will report an error, and only unPrivArr [1000]. aaa.but_check1 can be used at this time


Related articles: