Array.prototype defines many methods for operating arrays. Here are some methods in ECMAScript3
1.Array.join() method
This method converts the elements in the array into strings and connects them together according to the specified symbol, and returns the last generated string. It can contain a parameter, which is the symbol for connecting the array elements. The default is comma.
var ay = [1,2,3]; ay.join(); // =>"1,2,3" ay.join("+"); // => "1+2+3" ay.join(" "); // =>"1 2 3" ay.join(""); // =>"123" var by = new Array(10) //新建一个长度为10的空数组 by.join("-"); //=> "---------" 连接10个空元素
2.Array.reverse() method
This method reverses the order of the elements in the array and returns the array in reverse order. This method will change the current array and will not create a new array.
var a = [1,2,3];
a.reverse().join(); //=>"3,2,1" , at this time a=[3,2,1]
3.Array.sort() method
This method sorts the elements in the array and returns the sorted array. When the sort() method takes no parameters, the array is sorted in alphabetical order. If the array contains undefined elements, they will be sorted to the end of the array.
var as = ["banana","cherry","apple"];
as.sort();
as.join(" "); //=>"apple banana cherry"
We can also pass a comparison function as a parameter to the sort() method to sort the array using the specified comparison function. If the return value of the comparison function is less than 0, the first parameter comes first. On the contrary, if the return value is greater than 0, the second parameter comes first. If the two parameter values are equal, 0
is returned.
var sy = [1111,222,4,33];
sy.sort(); sy.sort(function(a,b){
return a-b;
});
Note: It is most appropriate to use anonymous functions here, because they are only called once and there is no need to specify the function name
4.Array.concat() method
This method creates and returns a new array, connecting the original array elements and each element in the method to form a new array. This method does not recursively call the parameters in the method.
a.concat([4,5]); //=>"1,2,3,4,5"
a.concat([4,5],[6,7]); //=>"1,2,3,4,5,6,7"
a.concat(4,[5,[6,7]]); //=>"1,2,3,4,5,[6,7]"
This method returns a fragment or subarray of the specified array. This method can have two parameters, which specify the start and end positions of the fragment respectively. The returned array contains the element specified by the first parameter and all elements up to but not including the first The array element at the position specified by the two parameters. If there is only one parameter, it contains the specified start position to the end of the array. The parameter can be a negative value, indicating the position relative to the last element in the array. This method does not modify the array being called.
d.slice(1,-1); //=>"2,3,4"
d.slice(3); //=>"4,5"
d.slice(-3,-1); //=>"3,4"
6.Array.splice()方法
该方法是在数组中插入或删除元素的通用方法,该方法会修改原始数组。该方法可以包含多个参数,第一个参数指定要在数组中插入或删除的起始位置,第二个参数制定了删除元素的个数,若不指定则将起始位置以及后面元素全部删除,两个参数之后的参数指定了插入数组的元素,该方法返回由删除元素组成的数组。
var e = [1,2,3,4,5,6]; e.splice(4); //=> 返回[5,6] ; e是[1,2,3,4] e.splice(1,2); //=> 返回[2,3] ; e是[1,4] var f = [1,2,3,4,5]; f.splice(2,0,"a","b"); //=>返回[]; f是[1,2,a,b,3,4,5] f.splice(2,2,[6,7],3); //=>返回[a,b]; f是[1,2,[6,7],3,4,5]
7.push()和pop()方法
这两个方法将数组当做栈使用,push()方法是在数组尾部添加一个或多个元素,并返回数组的长度。pop()方法是删除数组的最后一个元素,减少数组长度并返回删除的值。
8.unshift()方法和shift()方法
这两个方法是在数组头部进行添加删除操作,unshift()方法是在数组头部添加一个或多个元素,返回数组长度。shift()方法是删除数组第一个元素并返回。
var a=[]; //[] a.push(1,2); //[1,2] a.pop(); //[1] a.unshift(2,3); //[2,3,1] a.shift(); //[3,1]
9.toString()和toLocaleString()方法
这两个方法是将数组每个元素转化为字符串,toString()是将每个元素转化为字符串并且输出用逗号隔开。toLocaleString()方法是数组每个元素调用toLocaleString()转化为字符串,并使用本地化分隔符连接。
下面在介绍几个ECMAScript5中特有的数组方法,在介绍方法之前首先做一个大致了解。大多数方法的第一个参数接受一个函数,并且对数组每个元素调用一次这个函数,如果说稀疏数组,不存在的元素不调用函数。大多数情况下,调用的函数使用三个参数:数组元素,元素的索引以及数组本身。
1.forEach()方法
该方法从头到尾遍历数组,数组每个元素都调用指定的函数。该方法在遍历完所有数组元素之前不会终止。若想提前终止,必须将forEach()放到try块中,并可以抛出异常。
var data=[1,2,3,4,5] var sum = 0; data.forEach(function(value){ //=>value为数组元素 sum+=value; }) //=>15 data.forEach(function(value,i,a){ //=>三个参数分别指代数组元素,元素索引和数组 a[i] = v+1; }) //=>data=[2,3,4,5,6]
2.map()方法
该方法将数组的每个元素传递给指定的函数,并返回一个新数组,该数组包含了数组元素调用函数对应的返回值。如果是稀疏数组,返回的新数组也是同样结构的系数数组。
var a=[1,2,3]; var b=a.map(function(v){ return v*v; }) //=> b=[1,4,9]
3.filter()方法--类似于条件筛选
该方法返回的是原始数组的一个子集,传递的函数用来做逻辑判定,返回true或false,如果返回的值为true或可以转化为true,则当前数组元素就是子集的成员,添加到返回的数组中。该方法会跳过稀疏数组的空元素。
var a=[5,4,3,2,1] var smalla=a.filter(function(v){ return v<3; }) //=>返回[2,1] var everya=a.filter(function(v,i){ //=>i表示元素索引 return i%2==0; }) //=>返回[5,3,1]
4.every()和some()方法
这两个方法是对数组进行逻辑判定,对数组每个元素运用指定函数进行判定返回true或false。
every()方法是当且仅当数组中所有元素调用判定函数都返回true,才返回true,否则返回false。
some()方法是当数组中至少有一个元素调用判定函数返回true,就返回true,否则返回false。
这两个方法都是一旦确认返回值后就不在遍历数组元素了。
5.reduce()和reduceRight()方法
这两个方法使用指定的函数将数组元素进行组合,生成单个值。
reduce()需要两个参数,第一个是执行化简组合的操作函数,第二个是组合的初始值。和前面几个方法不同的是,常见的三个参数(数组元素、元素索引和数组本身)会作为操作函数的2~4个参数传递给函数,第一个参数是到目前为止进行计算组合的结果。
如果是针对空数组,并不指定初始值时调用reduce()方法会导致类型错误异常。
reduceRight()方法和reduce()方法的工作原理相同,不同的是其按数组索引从高到低进行处理(即从右到左进行合并处理)
6.indexOf()和lastIndexOf()方法
这两个方法都是用于在整个数组中搜索具体给定的值,并返回第一个匹配元素的索引值,若没有则返回-1.indexOf()方法是从头到尾进行搜索,而lastIndexOf()是从尾到头进行搜索。

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

在Go语言中,数组是一种重要的数据类型。它与其他语言的数组一样,是一组相同类型的数据组成,可以通过一个索引来访问数组中的元素。在某些情况下,我们需要从一个数组中删除元素,本文将会介绍在Go语言中如何实现数组删除。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.