concat(array1, array2, ...)

作用:连接两个或多个数组,并返回新的数组。

var hege = ['Cecilie', 'Lone']
var stale = ['Emil', 'Tobias', 'Linus']
var kai = ['Robin']
var children = hege.concat(stale, kai)
console.log(children) // ["Cecilie", "Lone", "Emil", "Tobias", "Linus", "Robin"]

 

slice(start, end)

作用:选取数组的一部分,并作为数组返回。

提示:从 start 开始,到 end 之前。

var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
var citrus = fruits.slice(1,3)
console.log(citrus) // ["Orange", "Lemon"]

 

splice(start, 删除多少个元素, 要添加的元素)

作用:添加、删除或替换数组元素。

提示:作为删除,返回删除元素组成的数组,否则返回空数组。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.splice(2, 0, 'Lemon', 'Kiwi')
console.log(fruits) // ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]

 


 

copyWithin(替换位置, start, end)

作用:将数组从某个位置开始的所有元素替换成指定位置开始的元素。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.copyWithin(2, 0)
console.log(fruits) // ["Banana", "Orange", "Banana", "Orange"]
var fruits = ['Banana', 'Orange', 'Apple', 'Mango', 'Kiwi', 'Papaya']
fruits.copyWithin(2, 0, 2)
console.log(fruits) // Banana,Orange,Banana,Orange,Kiwi,Papaya

 


 

entries()

作用:返回一个包含数组元素下标和元素的迭代对象。

for (let [index, elem] of ['a', 'b'].entries()) {
  console.log(index, elem)
}
// 0 "a"
// 1 "b"

 

keys()

作用:返回一个包含数组下标的迭代对象。

for (let index of ['a', 'b'].keys()) {
  console.log(index);
}
// 0
// 1

 

values()

作用:返回一个包含数组元素的迭代对象。

for (let elem of ['a', 'b'].values()) {
  console.log(elem);
}
// 'a'
// 'b'

 


 

every(function(item, index, array), thisValue)

作用:对数组中的每一项运行给定函数,如果函数对每一项都返回 true,则最后返回 true,否则返回 false 。

var ages = [32, 33, 16, 40]
var result = ages.every(function checkAdult(age) {
  return age >= 18
})
console.log(result) // false

 

filter(function(item, index, array), thisValue)

作用:对数组中的每一项运行给定函数,返回函数会返回 true 的项组成的数组。

var ages = [32, 33, 16, 40]
var result = ages.filter(function checkAdult(age) {
  return age >= 18
})
console.log(result) // [32, 33, 40]

 

forEach(function(currentValue, index, arr), thisValue)

作用:对数组中的每一项运行给定函数,没有返回值。

var numbers = [4, 9, 16, 25]
numbers.forEach(function myFunction(item, index) {
  console.log(item, index)
})

 

map(function(currentValue, index, arr), thisValue)

作用:对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。

var numbers = [1, 2, 3, 4, 5]
var result = numbers.map(function(item, index, array) {
  return item * 2
})
console.log(result) // [2, 4, 6, 8, 10]

 

some(function(item, index, array), thisValue)

作用:对数组中的每一项运行给定函数,如果函数对有一项返回 true,则返回 true,否则返回 false。

var numbers = [1, 2, 3, 4, 5]
var result = numbers.some(function(item, index, array) {
    return item > 2
})
console.log(result) // true

 

find(function(item, index, array), thisValue)

作用:对数组中的每一项运行给定函数,返回符合条件的第一个数组元素,没有则返回 undefined 。

var ages = [3, 10, 18, 20]
var result = ages.find(function checkAdult(age) {
    return age >= 18
})
console.log(result) // 18

 

findIndex(function(item, index, array), thisValue)

作用:对数组中的每一项运行给定函数,返回符合条件的第一个数组元素的位置,没有则返回-1。

var ages = [3, 10, 18, 20]
var result = ages.findIndex(function checkAdult(age) {
    return age >= 18
})
console.log(result) // 2

 


 

fill(填充元素, start, enx)

作用:使用一个固定值来填充数组。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.fill('Runoob')
console.log(fruits) // ["Runoob", "Runoob", "Runoob", "Runoob"]
var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.fill('Runoob', 2, 4)
console.log(fruits) // ["Banana", "Orange", "Runoob", "Runoob"]

 


 

from(类数组对象, mapFunction, thisValue)

作用:将类数组对象转换为数组,否则返回空数组。

var arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
}

// ES5的写法
var arr1 = [].slice.call(arrayLike)
console.log(arr1) // ["a", "b", "c"]

// ES6的写法
var arr2 = Array.from(arrayLike)
console.log(arr2) // ["a", "b", "c"]

 


 

includes(指定值, start)

作用:判断一个数组是否包含一个指定的值。

console.log([1, 2, 3].includes(2)) // true
console.log([1, 2, 3].includes(4)) // false
console.log([1, 2, NaN].includes(NaN)) // true
console.log([1, 2, 3].includes(3, 3)) // false
console.log([1, 2, 3].includes(3, -1)) // true

 

indexOf(item)

作用:返回指定元素的在数组中的位置,没有则返回-1。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var index = fruits.indexOf('Apple')
console.log(index) // 2
var fruits=['Banana', 'Orange', 'Apple', 'Mango', 'Banana', 'Orange', 'Apple']
var index = fruits.indexOf('Apple', 4)
console.log(index) // 6

 

lastIndex(item)

作用:返回指定元素的在数组中最后出现的位置,没有则没有-1。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var index = fruits.lastIndexOf('Apple')
console.log(index) // 2

 


 

join(str)

作用:将数组按照字符分隔转换为字符串。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(); // Banana,Orange,Apple,Mango

 


 

pop()

作用:删除并返回数组的最后一个元素。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var result = fruits.pop()
console.log(result) // "Mango"

 

push(item)

作用:向数组的末尾添加一个或更多元素,并返回新数组的长度。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var result = fruits.push('Kiwi')
console.log(result) // 5

 

shift()

作用:删除并返回数组的第一个元素。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var result = fruits.shift()
console.log(result) // "Banana"

 

unshift(item)

作用:向数组的开头添加一个或更多元素,并返回新数组的长度。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
var result = fruits.unshift('Lemon', 'Pineapple')
console.log(result) // 6

 


 

reduce(function(total, currentValue, currentIndex, arr), initialValue)

作用:将数组元素从左到右计算为一个值。

var arr = [1, 2, 3]
var reduceArray = arr.reduce(function(acc, current) {
  acc.push(current * 2)
  return acc
}, [])
console.log(reduceArray) // [2, 4, 6]

 

reduceRight(function(total, currentValue, currentIndex, arr), initialValue)

作用:将数组元素从右到左计算为一个值。

var arr = [1, 2, 3]
var reduceArray = arr.reduceRight(function(acc, current) {
  acc.push(current * 2)
return acc
}, [])
console.log(reduceArray) // [6, 4, 2]

 


 

reverse()

作用:反转数组中元素的顺序。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.reverse()
console.log(fruits) // ["Mango", "Apple", "Orange", "Banana"]

 

sort(排序函数)

作用:对数组元素进行排序。

var fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits.sort()
console.log(fruits) // ["Apple", "Banana", "Mango", "Orange"]
var values = [0, 1, 5, 10, 15]
values.sort(function compare(value1, value2) {
if (value1 < value2) {
    return -1
  } else if (value1 > value2) {
    return 1
  } else {
    return 0
  }
})
console.log(values) // [0, 1, 5, 10, 15]

 

posted on 2019-09-21 14:49  依旧那片天  阅读(179)  评论(0编辑  收藏  举报