Js array various methods

1. The array length property

In Arraythe object of which there is a lengthproperty that can be used to view the length of the array.

E.g:

var arr = [10,20,30,40]; console.log(arr.length);// 4 


通常情况下,我们可以通过将length属性值设置为0,来让数组清空。

E.g:

var arr = [10,20,30,40]; arr.length = 0; console.log(arr); // [] 数组被清空 

用length的增删改
增:直接选定,哪里没有增哪里
删:只能删后面不能删前面
改:选定哪个改哪个
查:选定哪个查哪个

2. The method of operating an array among other

首先来说下push()方法。通过这个方法,我们能够将一个或者多个元素添加到数组的末尾,并且返回数组的新长度。
// 创建一个数组 var arr1 = ['张三','李四'];
// 通过Push方法向数组中添加元素 arr1.push("王五");
// 检查数组内容 console.log(arr1); // [ '张三', '李四', '王五' ]
// 尝试添加多个内容 arr1.push("赵六","刘七");
// 打印数组元素 console.log(arr1);
// [ '张三', '李四', '王五', '赵六', '刘七' ]
// 创建一个新的数组 var arr2 = [1,2,3,4];
// 尝试将arr2添加到arr1 arr1.push(arr2);
// 打印arr1; console.log(arr1);
// [ '张三', '李四', '王五', '赵六', '刘七', [ 1, 2, 3, 4 ] ]
// 向arr1中再次添加数据并且查看push方法的返回值 console.log(arr1.push('hello,world')); // 7

pop()方法能够从数组中删除最后一个元素,并且返回该元素的值。此方法更改数组的长度。

var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

Above, we demonstrated poplast element method to delete an array, but in the use of popthe method, we need to note that the method lengthused to determine the position of the last element of the property,
if you do not include the length property or the length property can not be converted into a numerical value then will the length property is set to 0, and returns undefined.

At the same time, you call the pop method on an empty array will return undefined.

 

unshift()The method can be added to the beginning of one or more elements of the array, and returns the new length of the array.

var array1 = [1, 2, 3];

console.log(array1.unshift(4, 5)); // expected output: 5 console.log(array1); // expected output: Array [4, 5, 1, 2, 3]


shift()The method removes the first element from the array, and returns the value of the element. This method changes the length of the array.

var array1 = [1, 2, 3];

var firstElement = array1.shift(); console.log(array1); // expected output: Array [2, 3] console.log(firstElement); // expected output: 1

shift method of removing the element index of 0 (i.e., the first element), and returns the element is removed, along with the index value minus 1 other elements. If the property is 0. length (length 0), undefined is returned.

The method is not limited to an array of shift: This method can be similar to the array of objects through a method call or apply effect. But for no length attribute (from the last zero of a series of consecutive numbers properties) of the object, call the method might not make any sense.

 

 toString () values ​​to the array to an array (comma delimited) string directly into a string, and returns

 

join () method all array elements can also be incorporated into a string. Its behavior is similar to toString (), but you can also specify delimiters
var Fruits = [ "Banana", "Orange", "the Apple", "Mango"];
document.getElementById ( "Demo") the innerHTML = fruits.join. ( "*");
Banana Orange * * * the Apple Mango

 

concat () method to create a new array by combining (connecting) an existing array: concat () method does not change the existing array. It always returns a new array.
concat () method can be used any number of array parameters: var of arr1 = [ "Cecilie", "Lone"];
var arr2 is = [ "of Emil", "Tobias", "of Linus"];
var ARR3 = [ "Robin", "Morgan"];
var = myChildren arr1.concat (arr2 is, ARR3); // connected together arr1, arr2 and ARR3

concat () method may be used as the parameter values: var of arr1 = [ "Cecilie", "Lone"];
var myChildren arr1.concat = ([ "of Emil", "Tobias", "of Linus"]);

 

arr.splice (m, n, "") - the value of the array replacing the specified position, no assignment was to delete the bit value;       

m represents an index value   

insert the replacement of several n (0 represents insertion)

"" Representative values ​​can be replaced or inserted into a plurality of 

Delete the specified location, and replaced, return the deleted data

 

arr.slice (m, n) - m to be taken from a first n represents a value wherein if m from the end;

 

arr.sort (function) according to the default character from small to large; (function (a, b) {return ab or ba;}) can be converted into numeric sort;

 

indexOf(value,start);

Returns an array or a string of characters or a string of predetermined positions;

Query and return data index

value for the data to be queried; start is optional, represents the starting position of the query, when the start is negative, the number of forwardly from the end of the array; not present if the query value, the method returns -1

lastIndexOf () returns the inverse query and index data

 

 

3.ES5 new array method

 Loop, through the array 

forEach(callback); 

Parameter, callback, the callback function, all entries will traverse the array, the callback function takes three parameters, namely value, index, self; forEach no return value

 

 map(callback);

ForEach same, while the data callback function returns, to form a new array returned by the Map;

 

filter(callback);

With forEach, while the callback function returns a Boolean value of true data returned by the filter to form a new array   

 

 

arr.every(callback)

判断数组中每一项是否都满足条件,只有所有项都满足条件,才会返回true 。   

 

arr.some(callback) 

判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。 

 

 

arr.reduce()
功能:从数组的第一项开始,逐个遍历到最后,迭代数组的所有项,然后构建一个最终返回的值。
参数:reduce()接收一个或两个参数:第一个是回调函数,表示在数组的每一项上调用的函数;第二个参数(可选的)作为归并的初始值,被回调函数第一次执行时的第一个参数接收。
reduce(callback,initial);callback默认有四个参数,分别为prev,now,index,self。
callback返回的任何值都会作为下一次执行的第一个参数。
如果initial参数被省略,那么第一次迭代发生在数组的第二项上,因此callback的第一个参数是数组的第一项,第二个参数就是数组的第二项。

 

arr.reduceRight()
功能:(与reduce类似)从数组的最后一项开始,向前逐个遍历到第一位,迭代数组的所有项,然后构建一个最终返回的值。
参数:同reduce。
demo:同reduce

             

 

Guess you like

Origin www.cnblogs.com/peihang/p/11442205.html