This article will introduce you to the common properties of array objects in Javascript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
concat()
is used to concatenate two or more arrays. This method does not modify the existing array, but simply returns a copy of the concatenated array.
var a = ["aa","ccc"]; var b = ["vv","mm"]; var c = ["123"]; var d = a.contac(b,c); ==> ["aa","ccc","vv","mm","123"]
every()
is used to detect whether all elements of the array meet the specified conditions (provided through the function).
every() method uses the specified function to detect all elements in the array:
If an element in the array is detected to be unsatisfactory, the entire expression returns false , and the remaining elements will not be tested again.
Returns true if all elements meet the condition.
Note: every() will not detect empty arrays.
Note: every() will not change the original array.
var ary = [3,4,5,6,7]; var result = ary.every(function(item){ return item>5; }) ==> false
filter()
Create a new array. The elements in the new array are checked by checking all elements in the specified array that meet the conditions. .
Note: filter() will not detect empty arrays.
Note: filter() does not change the original array.
var ary = [2,3,5,6,7]; var result = ary.filter(function(item){ return item>3; }); ==> [5,6,7]
forEach()
is used to call each element of the array and pass the element to the callback function.
var ary = [3,4,5,6,7]; var result = ary.forEach(function(item,index){ console.log(item,index) });
includes()
is used to determine whether an array contains a specified value. If so, it returns true, otherwise false.
arr.includes(searchElement) arr.includes(searchElement, fromIndex) // searchElement 要查找的元素 // fromIndex 开始查找的位置,默认为0;如果fromIndex 大于等于数组长度 ,则返回 false ,该数组不会被搜索 //如果 fromIndex 为负值,计算出的索引将作为开始搜索searchElement的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
var arr = ['a', 'b', 'c']; arr.includes('a'); // true arr.includes('a', -100); // true
indexOf()
can return a specified element position in the array. Returns -1 if the specified element is not found in the array.
var num = [1,2,3,4,'Apple']; var a = num.indexOf("Apple"); //4
lastIndexOf()
can return the position where a specified element last appears in the array. The specified position in an array starts from Search backward and forward. If the element to be retrieved does not appear, the method returns -1.
var num = [1,2,3,4,'Apple']; var a = num.lastIndexOf("Apple"); //4
isArray()
is used to determine whether an object is an array. Returns true if the object is an array, false otherwise.
var item = []; Array.isArray(item); //true
join()
is used to convert all elements in the array into a string. Elements are separated by the specified delimiter.
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var energy = fruits.join(); //Banana,Orange,Apple,Mango var energy = fruits.join("|"); //Banana|Orange|Apple|Mango
map()
Returns a new array, and the elements in the array are the values of the original array elements after calling the function.
The map() method processes elements in sequence according to the order of the original array elements.
Note: map() will not detect empty arrays.
Note: map() will not change the original array.
var ary= [4,9,16,25]; var result = ary.map(Math.sqrt); //result ==> 2,3,4,5
pop()
Used to delete the last element of the array and return the deleted element.
var ary = [2,4,5,6]; var del = ary.pop(); //del ==> 6 // ary ==> 2,4,5
push()
Can add one or more elements to the end of the array and return the new length.
var ary = ['aa','bb','cc']; var result = ary.push('ss'); //result ==> 4 //ary ==> ['aa','bb','cc','ss']
shift()
is used to delete and return the first element of the array.
var ary = [2,4,5,6]; var del = ary.shift(); //del ==> 2 // ary ==> 4,5,6
unshift()
Adds one or more elements to the beginning of the array and returns the new length.
var ary = ['aa','bb','cc']; var result = ary.unshift('ss'); //result ==> 4 //ary ==> ['ss','aa','bb','cc']
reduce()
Receives a function as an accumulator, and starts reducing each value in the array (from left to right), Finally calculated as a value.
This function must receive two parameters, reduce()
The result continues to be cumulatively calculated with the next element of the sequence.
Note: reduce() will not execute the callback function for an empty array.
var arr = [1, 3, 5, 7, 9]; arr.reduce(function (x, y) { return x + y; }); // 25
reduceRight() The function of the method is the same as that of reduce(). The difference is that reduceRight() moves the elements in the array forward from the end of the array. Array items are accumulated.
reverse()
is used to reverse the order of elements in an array.
var fruits = [1, 2, 3, 4]; fruits.reverse(); // [4,3,2,1]
slice()
Returns selected elements from an existing array.
The slice() method can extract a certain part of the string and return the extracted part as a new string.
Note: The slice() method does not change the original array.
Returns a new array containing the elements in arrayObject from start to end (excluding this element).
array.slice(start, end) //start 可选。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。 //end 可选。规定从何处结束选取。 //该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。 //如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1,3); // ["Orange", "Lemon"]
some()
用于检测数组中的元素是否满足指定条件(函数提供)。
some() 方法会依次执行数组的每个元素:
如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
如果没有满足条件的元素,则返回false。
var arr = [1, 3, 5, 7, 9]; arr.some(function (item) { return item>4; }); // true
sort()
用于对数组的元素进行排序。返回新的数组
排序顺序可以是字母或数字,并按升序或降序。默认排序顺序为按字母升序。
注意:当数字是按字母顺序排列时"40"将排在"5"前面。
使用数字排序,你必须通过一个函数作为参数来调用。
函数指定数字是按照升序还是降序排列。
注意: 这种方法会改变原始数组!。
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // Apple,Banana,Mango,Orange //升序 var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b}); //1,5,10,25,40,100 //降序 var points = [40,100,1,5,25,10]; points.sort(function(a,b){return b-a}); // 100,40,25,10,5,1
splice()
用于插入、删除或替换数组的元素。
注意:这种方法会改变原始数组!
array.splice(index,howmany,item1,.....,itemX) //index 必需。规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。 //howmany 必需。规定应该删除多少元素。必须是数字,但可以是 "0"。如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。 //item1, ..., itemX 可选。要添加到数组的新元素 //如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。
//移除数组的第三个元素,并在数组第三个位置添加新元素: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,1,"Lemon","Kiwi"); //["Banana", "Orange", "Lemon","Kiwi","Mango"] //从第三个位置开始删除数组后的两个元素: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,2); //["Banana", "Orange"]
toString()
可把数组转换为字符串,并返回结果。
注意: 数组中的元素之间用逗号分隔。
array.toString()
valueOf()
返回 Array 对象的原始值。
该原始值由 Array 对象派生的所有对象继承。
valueOf() 方法通常由 JavaScript 在后台自动调用,并不显式地出现在代码中。
注意: valueOf() 方法不会改变原数组。
//valueOf() 是数组对象的默认方法。 // fruits.valueOf()与 fruits返回值一样。 var fruits = ["Banana", "Orange", "Apple", "Mango"]; var v=fruits.valueOf();
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程!
相关推荐:
The above is the detailed content of A brief discussion on common properties of array objects in Javascript. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.