Home  >  Article  >  Web Front-end  >  A brief discussion on common properties of array objects in Javascript

A brief discussion on common properties of array objects in Javascript

青灯夜游
青灯夜游forward
2018-10-12 16:46:122231browse

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视频教程

相关推荐:

JavaScript图文教程

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete