Home Web Front-end JS Tutorial Basic usage of js arrays and removing elements from arrays based on subscripts (numeric values ​​or characters)_javascript skills

Basic usage of js arrays and removing elements from arrays based on subscripts (numeric values ​​or characters)_javascript skills

May 16, 2016 pm 05:19 PM
js array Remove element

1. Create an array

Copy the code The code is as follows:

var array = new Array();
var array = new Array(size);//Specify the length of the array
var array = new Array(item1,item2...itemN);//Create an array and assign values

2. Value acquisition and assignment
Copy code The code is as follows:

var item = array[index ];//Get the value of the specified element
array[index] = value;//Assign a value to the specified element

3. Add new elements
Copy code The code is as follows:

array.push(item1,item2...itemN);//Add one or more elements to the array, Return the length of the new array
array.unshift(item1,item2...itemN);//Add one or more elements to the beginning of the array, the original element position will automatically move backward, and return the length of the new array
array.splice(start,delCount,item1,item2...itemN);//Delete delCount elements backward from the start position, and then insert one or more new elements from the start position

4. Delete elements
Copy code The code is as follows:

array.pop() ;//Delete the last element and return the element
array.shift();//Delete the first element, the array element position will automatically move forward, and return the deleted element
array.splice(start, delCount);//Delete delCount elements backward from the start position

5. Merging and intercepting arrays
Copy code The code is as follows:

array.slice(start,end);//Return a part of the array in the form of an array. Note that the element corresponding to end is not included. If it is omitted end will copy all elements after start
array.concat(array1,array2); // Splice multiple arrays into one array

6. Sorting of arrays
Copy code The code is as follows:

array.reverse();//Array reverse
array.sort( );//Array sorting, return array address

7. Convert array to string
Copy code The code is as follows:

array.join(separator);//Connect the arrays with separator

I have listed all of them, but I still can’t find it. Delete the array based on the subscript. Elemental approach! So I checked some information and found a solution.
Deleting array elements requires extending the Array prototype prototype.
Generally, the subscripts of arrays are numerical, but there are also character subscripts.
To process numerical types, first write the following code, It is an extension of array
Copy code The code is as follows:

Array.prototype.del = function (dx)
{
if(isNaN(dx)||dx>this.length){return false;}
this.splice(dx,1);
}

Secondly, numeric parameters can be passed directly. For example, var arr = ["aa","bb"];arr.del(0);
Let’s talk about character subscripts
Copy code The code is as follows:

var arr = [].
arr["aa"] = 1;
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove elements from es6 array How to remove elements from es6 array Jan 11, 2023 pm 03:51 PM

How to remove elements from es6 array

How to get the length of an array in js How to get the length of an array in js Jun 20, 2023 pm 05:33 PM

How to get the length of an array in js

How to convert string to array in javascript How to convert string to array in javascript Nov 23, 2022 pm 07:28 PM

How to convert string to array in javascript

Can js array be converted into php array? Can js array be converted into php array? Jun 02, 2023 am 10:06 AM

Can js array be converted into php array?

There are several ways to delete an element from a js array There are several ways to delete an element from a js array Aug 02, 2023 am 10:09 AM

There are several ways to delete an element from a js array

Remove an element from the end of an array using the PHP array_pop() function Remove an element from the end of an array using the PHP array_pop() function Jun 27, 2023 am 10:18 AM

Remove an element from the end of an array using the PHP array_pop() function

JS array sorting: how to use the sort() method JS array sorting: how to use the sort() method Dec 27, 2023 pm 03:40 PM

JS array sorting: how to use the sort() method

How to remove elements from array in PHP How to remove elements from array in PHP Jul 08, 2023 pm 04:57 PM

How to remove elements from array in PHP

See all articles