Detailed explanation of core JavaScript array methods
The following are some commonly used JavaScript array methods:
push()
: Adds one or more elements to the end of the array and returns the length of the new array.
<code class="language-javascript">let numbers = [1, 2, 3]; console.log(numbers.push(4)); // 输出:4 numbers数组变为 [1, 2, 3, 4]</code>
slice()
: Extract elements within the specified index range from the array and return a new array. The original array is not changed.
<code class="language-javascript">let numbers = [1, 2, 3, 4, 5]; console.log(numbers.slice(1, 4)); // 输出:[2, 3, 4] (从索引1到3)</code>
splice()
: Add or remove elements from the array. You can specify the starting index, the number of elements to delete, and the new elements to insert.
<code class="language-javascript">let fruits = ['apple', 'mango', 'orange', 'pear']; fruits.splice(2, 0, 'pawpaw', 'strawberries'); // 从索引2开始,不删除任何元素,插入'pawpaw'和'strawberries' console.log(fruits); // 输出:['apple', 'mango', 'pawpaw', 'strawberries', 'orange', 'pear'] fruits.splice(2, 1); // 从索引2开始,删除1个元素 console.log(fruits); // 输出:['apple', 'mango', 'strawberries', 'orange', 'pear']</code>
concat()
: Concatenates two or more arrays and returns a new array. The original array is not changed.
<code class="language-javascript">let arr1 = [1, 2, 3, 4]; let arr2 = [5, 6, 7]; console.log(arr1.concat(arr2)); // 输出:[1, 2, 3, 4, 5, 6, 7] let arr3 = [8, 9]; console.log(arr1.concat(arr2, arr3)); // 输出:[1, 2, 3, 4, 5, 6, 7, 8, 9]</code>
fill()
: Replaces elements in the specified range in the array with the specified value.
<code class="language-javascript">let arr4 = [1, 2, 3, 4]; arr4.fill('Anurag', 2, 4); // 从索引2到3,用'Anurag'填充 console.log(arr4); // 输出:[1, 2, 'Anurag', 'Anurag']</code>
shift()
: Delete the first element of the array and return that element.
<code class="language-javascript">let arr4 = [1, 2, 3, 4]; arr4.shift(); console.log(arr4); // 输出:[2, 3, 4]</code>
indexOf()
: Returns the index of the first occurrence of the specified element in the array. If the element does not exist, returns -1.
<code class="language-javascript">let arr4 = [1, 2, 3, 4]; console.log(arr4.indexOf(3)); // 输出:2</code>
lastIndexOf()
: Returns the index of the last occurrence of the specified element in the array. If the element does not exist, returns -1.
<code class="language-javascript">let arr4 = [1, 2, 3, 4, 3]; console.log(arr4.lastIndexOf(3)); // 输出:4</code>
includes()
: Determines whether the array contains the specified element and returns a Boolean value.
<code class="language-javascript">let arr4 = [1, 2, 3, 4]; console.log(arr4.includes(4)); // 输出:true console.log(arr4.includes(5)); // 输出:false</code>
pop()
: Delete the last element of the array and return that element.
<code class="language-javascript"> let arr4 = [1, 2, 3, 4]; arr4.pop(); console.log(arr4); // 输出:[1, 2, 3]</code>
join()
: Concatenates array elements into a string and returns the string. Delimiters can be specified.
<code class="language-javascript"> let arr4 = [1, 2, 3]; console.log(arr4.join('and')); // 输出:1and2and3</code>
unshift()
: Adds one or more elements to the beginning of the array and returns the length of the new array.
<code class="language-javascript"> let arr4 = [1, 2, 3]; arr4.unshift(0); console.log(arr4); // 输出:[0, 1, 2, 3]</code>
My GitHub link
My LinkedIn link (please replace with your actual link)
The above is the detailed content of Arrays in JavaScript. For more information, please follow other related articles on the PHP Chinese website!