Home  >  Article  >  Web Front-end  >  Frequently Asked Questions about Adding and Accessing Values ​​to Arrays in JavaScript_Javascript Tips

Frequently Asked Questions about Adding and Accessing Values ​​to Arrays in JavaScript_Javascript Tips

WBOY
WBOYOriginal
2016-05-16 15:16:11941browse

This article will introduce to you some minor issues about arrays, which may be helpful to you. This article is not well written, so please forgive me.

1.

// var arr = [,,];
// arr["bbb"]="nor ";
// arr[-]="nor ";
// console.log(arr); >> [, , , bbb: "nor ", -: "nor "]
// console.log(arr.bbb) >> "nor "

If we want to add a value to the array, add it in the form of []. If it is a negative number or a string, it is added at the end of the array, and it is added in the form of a key-value pair, so The next time you access this value, you can use the dot form to access it, but if it is a number, you must access it through [].

2.

// var arr = [,,];
// arr["bbb"]="nor ";
// console.log(arr); [, , , bbb: "nor "]
// console.log(arr[]) undefined

If you add a value to the array through a string or a negative number, the next time you access it, it must also be accessed through a key-value pair

3.

// var arr = [,,];
// arr["bbb"]="nor ";
// arr[-]=;
// arr.push();
// console.log(arr); >> [, , , , bbb: "nor "]
// console.log(arr.length); >> 

// It is worth noting that the value added through a string or a negative number will not add its length to the array, and the value added in this way will always be at the end of the array because we use the push method When adding the number 4, we found that it did not add it to the end. Everyone knows that the push method adds the value to the end of the array. Maybe we can draw a conclusion that numbers and numbers are arranged, key-value pairs and key-value pairs are arranged.

This article uses the above three points to briefly analyze the common problems of adding values ​​and accessing values ​​​​in arrays in JavaScript. I hope it will be helpful to everyone. At the same time, the editor of Script House would like to wish everyone a happy Spring Festival!

Statement:
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