Array数组常用的内置方法

Array.concat()

concat() 方法用于连接两个或多个数组。

var array1 = ['a', 'b'];
var array2 = [ 'c','d'];
console.log(array1.concat(array2)); 
//['a', 'b', 'c','d']
 

Array.every()

every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。

function isBelowThreshold(currentValue) {
  return currentValue < 40;
}
var array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// true

Array.filter()

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
The filter() method creates a new array with all elements that pass the test implemented by the provided function.

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);//  ["exuberant", "destruction", "present"]

Array.find()

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

var array1 = [5, 12, 8, 130, 44];

var found = array1.find(function(element) {
  return element > 10;
});

console.log(found);
// expected output: 12

Array.findIndex()

findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。如果没有符合条件的元素返回 -1

var array1 = [5, 12, 8, 130, 44];
function findFirstLargeNumber(element) {
  return element > 13;
}
console.log(array1.findIndex(findFirstLargeNumber));
// expected output: 3

Array.forEach()

var array1 = ['a', 'b', 'c'];

array1.forEach(function(element) {
  console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

Array.includes()

includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。

var array1 = [1, 2, 3];
console.log(array1.includes(2));  // expected output: true
var pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));  // expected output: true
console.log(pets.includes('at'));  // expected output: false

Array.indexOf()

indexOf() 方法可返回数组中某个指定的元素位置。如果在数组中没找到指定元素则返回 -1。

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));  // 1

console.log(beasts.indexOf('bison', 2));  // 4

console.log(beasts.indexOf('giraffe'));  //  -1

//找出指定元素出现的所有位置
var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array.indexOf(element);
while (idx != -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]

Array.join()

join()方法将一个数组或(类数组对象)的所有元素连接成一个字符串,并返回这个字符串

var elements = ['one', 'two', 'three'];

console.log(elements.join());  //  one,two,three

console.log(elements.join(''));  // onetwothree

console.log(elements.join('-'));  // one-two-three

Array.lastIndexOf()

lastIndexOf()方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从formIndex处开始

var animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

console.log(animals.lastIndexOf('Dodo'));
// 3

console.log(animals.lastIndexOf('Tiger'));
//  1

Array.map()

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

var array1 =[2, 3, 4, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);  //[4, 6, 8, 32]

Array.pop()

pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。  
var plants = ["run", "yi", "ke", "ji", "hello"];
console.log(plants.pop()); //hello
console.log(plants);   //["run", "yi", "ke", "ji"]
plants.pop();
console.log(plants); //["run", "yi", "ke"]

Array.push()

push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。

var arr=['hello','run','yi'];
arr.push('keji')
console.log(arr); //["hello", "run", "yi", "keji"]

Array.shift()

shift() 方法从数组中删除第一个元素,并返回该元素的值。

var array1 = [1, 2, 3];
var firstElement = array1.shift();

console.log(array1);//  [2, 3]
console.log(firstElement);// 1

Array.unshift()

unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。

var company = ["run", "yi", "ke", "ji"];
company.unshift("hello");
console.log(company)//["hello", "run", "yi", "ke", "ji"]

Array.reduce()

方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

var arr=[1,2,3,4,5];
const reducer=(a,b)=>a+b;
console.log(arr.reduce(reducer))  //15
console.log(arr.reduce(reducer,100)) //115

Array.reverse()

reverse() 方法用于颠倒数组中元素的顺序。

var arr=['I','LOVE','YOU'];
arr.reverse();
console.log(arr) //["YOU", "LOVE", "I"]

Array.sclice()

sclice()方法返回一个新的数组对象,这一对象是一个由 begin和 end(不包括end)决定的原数组的浅拷贝。原始数组不会被改变。

var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
//  ["camel", "duck"]

console.log(animals.slice(1, 4));
// ["bison", "camel", "duck"]

//slice(1,4) 提取原数组中的第二个元素开始直到第四个元素的所有元素 (索引为 1, 2, 3的元素)。
//用俗话可以说是“含头不含尾”

Array.sort()

方法用原地算法对数组的元素进行排序,并返回数组。排序算法现在是稳定的。默认排序顺序是根据字符串Unicode码点。

var strarr=['one','two','three','four','five'];
strarr.sort();
console.log(strarr); //["five", "four", "one", "three", "two"]

var numarr=[1, 30, 4, 21];
numarr.sort();
console.log(numarr); //[1, 21, 30, 4]
//要比较数字而非字符串,比较函数可以简单的以 a 减 b,如下的函数将会将数组升序排列
numarr.sort(function(a,b){
    return a-b
})
console.log(numarr); //[1, 4, 21, 30]

Array.splice()

splice()方法通过删除现有元素和/或添加新元素来修改数组,并以数组返回原数组中被修改的

var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
//  ['Jan', 'Feb', 'March', 'April', 'June']

months.splice(4, 1, 'May');
console.log(months);
//  ['Jan', 'Feb', 'March', 'April', 'May']

Array.toString()

toString()返回一个字符串,表示指定的数组及其元素。

var array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
//  "1,2,a,1a"

猜你喜欢

转载自blog.csdn.net/weixin_33857679/article/details/87524026
今日推荐