Home >Web Front-end >Front-end Q&A >javascript array usage
Javascript is a common programming language and one of the most popular languages for front-end development. In front-end development, arrays are one of the most common data types used to store and manipulate a set of data. In this article, we will introduce the basic usage of arrays in JavaScript.
1. Create an array
You can use the following two methods to create an array in JavaScript:
a. Use array literals
Array literals are A set of values surrounded by square brackets [] can be defined by comma separation:
let fruits = ['apple','orange','banana'];
b. Use the new keyword
When using the new keyword to create an array, you need to specify the elements in the array Number:
let cars = new Array(3); //创建一个大小为3的数组
2. Access array elements
Use subscripts in JavaScript to access array elements. Array subscripts start from 0 and increase sequentially. For example:
console.log(fruits[0]); //输出"apple" console.log(cars[1]); //输出undefined,因为数组cars中没有元素
3. Modify the array element
You can modify the value of the array element through the subscript:
fruits[1] = 'grape'; console.log(fruits); //输出["apple","grape","banana"]
4. Get the array length
Yes Use the length property to get the length of the array:
console.log(fruits.length); //输出3 console.log(cars.length); //输出3,因为数组cars中有3个undefined元素
5. Add elements to the end of the array
You can use the push() method to add elements to the end of the array:
fruits.push('pear'); console.log(fruits); //输出["apple","grape","banana","pear"]
6. Delete the elements at the end of the array
You can use the pop() method to delete the elements at the end of the array:
fruits.pop(); console.log(fruits); //输出["apple","grape","banana"]
7. Iterate the array
You can use the for loop to iterate The elements in the array and perform the same operation on each element:
for(let i=0; i<fruits.length; i++){ console.log(fruits[i]); }
You can use the forEach() method to iterate over the elements in the array and perform a different operation on each element:
fruits.forEach(function(fruit){ console.log(fruit); });
8. Find the position of the element in the array
You can use the indexOf() method to find the position of the element in the array:
console.log(fruits.indexOf('banana')); //输出2
If the element is not in the array, -1 is returned. You can use the lastIndexOf() method to find elements starting from the end of the array.
9. Sort the array
You can use the sort() method to sort the array:
let numbers = [2,1,3,5,4]; numbers.sort(); console.log(numbers); //输出[1,2,3,4,5]
10. Connect the array
You can use concat() Method to connect two or more arrays:
let vegetables = ['carrot','cauliflower']; let food = fruits.concat(vegetables); console.log(food); //输出["apple","grape","banana","carrot","cauliflower"]
Summary
This article introduces some basic usage of JavaScript arrays, including creating arrays, accessing array elements, modifying array elements, Get the length of an array, add and remove array elements, iterate over an array, find element positions, sort arrays, and concatenate arrays. Arrays are an important part of JavaScript programming, and familiarity with these usages can help us better utilize JavaScript to develop front-end applications.
The above is the detailed content of javascript array usage. For more information, please follow other related articles on the PHP Chinese website!