search
HomeWeb Front-endJS TutorialBasic usage of js arrays and removing elements from arrays based on subscripts (numeric values ​​or characters)_javascript skills

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
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
es6 数组怎么移除元素es6 数组怎么移除元素Jan 11, 2023 pm 03:51 PM

方法:1、用shift()删除第一个元素,语法“数组.shift()”;2、用pop()删除最后一个元素,语法“数组.pop()”;3、用splice()删除任意位置的元素,语法“数组.splice(位置,个数)”;4、用length删除尾部的N个元素,语法“数组.length=原数组长度-N”;5、直接赋予空数组“[]”来清空元素;6、用delete删除指定下标的一个元素。

js获取数组长度的方法js获取数组长度的方法Jun 20, 2023 pm 05:33 PM

在 JS 中获取数组长度非常简单,每个数组都有一个 length 属性,该属性返回数组的最大长度,即其值等于最大下标值加 1。由于数字下标必须小于 2^32-1,所以 length 属性最大值等于 2^32-1。下面代码定义了一个空数组,然后为下标等于 100 的元素赋值,则 length 属性返回 101。因此,length 属性不能体现数组元素的实际个数。

javascript如何将字符串转为数组javascript如何将字符串转为数组Nov 23, 2022 pm 07:28 PM

3种转换方法:1、使用split(),可将给定字符串拆分为字符串数组,语法“str.split(分隔符,数组最大长度)”;2、利用扩展运算符“...”,可迭代字符串对象,将其转为字符数组,语法“[...str]”;3、使用Array.from(),可将字符串转为数组,语法“Array.from(str)”。

js数组可以转化成php数组吗js数组可以转化成php数组吗Jun 02, 2023 am 10:06 AM

js数组可以转化成php数组,其操作方法是:1、建立php示例文件;2、使用语法“JSON.stringify()”将js数组转化为JSON格式的字符串;3、使用语法“json_decode()”将JSON格式字符串转为PHP数组,此处添加了参数true,表示将JSON格式字符串转换成PHP关联数组。

使用PHP array_pop()函数从数组末尾移除一个元素使用PHP array_pop()函数从数组末尾移除一个元素Jun 27, 2023 am 10:18 AM

在PHP中,数组是一种非常重要的数据类型,它可以存储多个值并进行一些基本的数据操作。数组通常需要添加、删除或修改其中的元素。本篇文章将介绍如何使用PHParray_pop()函数来从数组末尾移除一个元素。array_pop()函数是一个PHP内置函数,它可以从数组的末尾删除并返回一个元素。该函数用法如下:array_pop($array)其中,$array

js数组删除某个元素有几种方法js数组删除某个元素有几种方法Aug 02, 2023 am 10:09 AM

js数组删除某个元素有4种方法,分别是:1、使用splice;2、使用filter;3、使用pop方法和shift;4、使用delete关键字。

JS数组排序:sort()方法怎么用JS数组排序:sort()方法怎么用Dec 27, 2023 pm 03:40 PM

JavaScript的Array.prototype.sort()方法用于对数组的元素进行排序。此方法是就地排序,也就是说,它修改原始数组,而不是返回一个新的排序数组。默认情况下,sort()方法按照字符串Unicode码点值进行排序。这意味着它主要用于字符串和数字的排序,而不是用于对象或其他复杂数据类型的排序。

jquery怎么从末尾移除一个元素jquery怎么从末尾移除一个元素Nov 15, 2021 pm 05:23 PM

移除方法:1、利用“eq(-1)”选择器和remove()方法,语法“$(Element).eq(-1).remove()”;2、利用“:last”选择器和remove()方法,语法“$(Element:last).remove()”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software