//concat
// 链接两个或多个数组
var arr1 = [1,2,3];
var arr2 = [3,4,5];
var arr3 = arr1.concat(arr2);
console.log(arr1);//[1,2,3]
console.log(arr2);//[3,4,5]
console.log(arr3);//[1,2,3,3,4,5]
// join
// 把数组中所有元素放入字符串
var arr = [2,3,4];
console.log(arr.join());//2,3,4
console.log(arr);//[2,3,4]
// push
// 向数组末尾添加一个或多个元素,并返回新的长度
var a = [2,3,4];
var b = a.push(5);
console.log(a);//[2,3,4,5]
console.log(b);//4
// pop
// 删除并返回最后一个元素
var arr = [1,2,3];
console.log(arr.pop());//3
console.log(arr);//[1,2]
// shift
// 把第一个元素删除并返回第一个元素
var arr = [1,2,3];
console.log(arr.shift());//1
console.log(arr);//[2,3]
// unshift
// 可向数组开头添加一个或者更多元素,并返回馨长度
var arr = [2,3,4,5];
console.log(arr.unshift(4,7));//6
console.log(arr);//[4,7,2,3,4,5]
// slice
// 返回新数组,包含从start到end(不包含)的所有元素
var arr = [2,3,4,5];
console.log(arr.slice(0,3));//[2,3,4]
console.log(arr);//[2,3,4,5]
// splice
// 删除从index开始的0个或多个元素,并从参数列表中声明一个或多个值替换
// 被删除的元素
var a = [5,6,7,8];
console.log(a.splice(1,0,9));//[]
console.log(a);//[5,9,6,7,8]
var b = [5,6,7,8];
console.log(b.slice(1,2,3));//[6]
console.log(b);//[5,6,7,8]
// substring和substr
// 相同点:只写一个参数,都是截取字符串从当前下标以后直到
// 字符串最后的字符串片段
var str = '34567890';
console.log(str.substr(2));//567890
console.log(str.substring(2));//567890
// 不同点:substr(startindex,length),第二参数是截取字符串的长度
// substring(startindex,endindex),第二参数是截取字符串最终的下标(含头不含尾)
console.log(str.substr(3,5));//67890
console.log(str.substring(3,5));//67
// sort
// 按照Unicode code 位置排序默认升序
var fruit = ['cherries', 'apples' ,'oranges', 'bananas'];
console.log(fruit.sort());//['apples', 'bananas', 'cherries', 'oranges']
var scores = [1, 22, 10, 21, 2];
console.log(scores.sort());//[1, 10, 2, 21, 22]
var str = ['你', '我', '他'];
console.log(str.sort());
// reverse
// 颠倒排序并返回数组
var arr = [3,5,7];
console.log(arr.reverse());//[7,5,3]
console.log(arr);//[7,5,3]
// indexOf 和 lastIndexOf
// 接受查找的值和查找起始位置两个参数
// 不存在返回-1,存在返回位置,indeOf从前到后查找,lastIndexOf从后到前查找
var a = [2,9,9];
console.log(a.indexOf(2));// 0
console.log(a.indexOf(7));// -1
console.log(a.indexOf(9));//1
var b = [2,5,9,2];
console.log(b.lastIndexOf(2));//3
console.log(b.lastIndexOf(7));//-1
console.log(b.lastIndexOf(2,3));//3
console.log(b.lastIndexOf(2,2));//0
console.log(b.lastIndexOf(2,-2));//0
console.log(b.lastIndexOf(2,-1));//3
// every
// 对数组每一项都运行给定函数,若每一项都为true,则返回true
function isBigEnough (element, index, array) {
return element < 10;
}
var arr = [2,5,7,3,8,1];
console.log(arr.every(isBigEnough));
// some
// 对数组每一项都运行给定函数,任意一项返回true,则返回true
function isBigEnough (element, index, array) {
return element < 10;
}
var arr = [2,5,7,13,18,1];
console.log(arr.some(isBigEnough));
// filter
// 对每一项运行给定函数,返回结果为true的项组成数组
function isBigEnough (element, index, array) {
return element < 10;
}
var arr = [2,5,7,13,18,1];
console.log(arr.filter(isBigEnough));
// map
// 对每一项都运行给定函数,返回每次调用函数的结果组成新数组
var arr = [1,5,10,15];
var doubles = arr.map(function (x) {
return x * 2;
});
console.log(arr);//[1,5,10,15]
console.log(doubles);//[2,10,20,30]
// forEach
// 数组遍历
const items = ['item1', 'item2', 'item3'];
const copy = [];
console.log(copy);
items.forEach(function (item) {
copy.push(item);
})
console.log(copy);
// find()
// 传入回调函数,找到数组中符合当前搜索规则的第一个元素,返回并终止搜索
const arr = [1, '2', 3, '4', 5, '1', 2, '3', 4, '5'];
console.log(arr.find(n => typeof n === "number"));//1
console.log(arr.find(n => typeof n === "string"));//2
// findeIndex()
// 传入回调函数,找到数组符合当前搜索规则的第一个元素并返回下标
const arr = [1, '2', 3, '4', 5, '1', 2, '3', 4, '5'];
console.log(arr.findIndex(n => typeof n === "number"));//0
console.log(arr.findIndex(n => typeof n === "string"));//1
// fill()
// 用新元素替换数组内元素,可以指定替换下标范围
// arr.fill(value,start,end)
var arr = [2,4,6];
console.log(arr.fill(1,2,3));//[2,4,1]
// copyWithin()
// 选择数组某个下标,从该位置赋值元素,默认从0开始,也可以指定复制元素范围
// arr.copyWithin(target, start, end);
const arr = [1,2,3,4,5,6];
console.log(arr.copyWithin(4));//[1,2,3,4,1,2]
console.log(arr.copyWithin(3,1));//[1,2,3,2,3,4]
console.log(arr.copyWithin(3, 2, 4));//[1,2,3,3,2,4]
// from
// 将类似数组的对象和可遍历对象转为真正的数组
const arr = ['a', 'b', 'c'];
console.log(Array.from(arr));
console.log(Array.from('foo'));
// of
// 将一组值转为数组
console.log(Array());//[]
console.log(Array(3));//[ , , ,]
console.log(Array(3,11,8));//[3,11,8]
console.log(Array.of(7));//[7]
console.log(Array.of(1,2,3));//[1,2,3]
console.log(Array(7));//[ , , , , , , ]
console.log(Array(1,2,3));//[1,2,3]
// entries()
// 返回迭代器:返回键值对
// 数组
const arr = ['a','b','c'];
for (let v of arr.entries()) {
console.log(v);//[0,'a'] [1,'b'] [2,'c']
}
// set
const arr1 = new Set(['a','b','c']);
for (let v of arr1.entries()) {
console.log(v);//['a','a'] ['b','b'] ['c','c']
}
// map
const arr2 = new Map();
arr2.set('a','b');
arr2.set('b','b');
for (let v of arr2.entries()) {
console.log(v);//['a','b'] ['b','b']
}
// values()
// 返回迭代器:返回键值对的value
// 数组
const arr = ['a','b','c'];
for (let v of arr.values()) {
console.log(v);// 'a' 'b' 'c'
}
// set
const arr1 = new Set(['a','b','c']);
for (let v of arr1.values()) {
console.log(v);//'a' 'b' 'c'
}
// map
const arr2 = new Map();
arr2.set('a','a');
arr2.set('b','b');
for (let v of arr2.values()) {
console.log(v);//'a' 'b'
}
// keys()
// 返回迭代器:返回键值对的key
// 数组
const arr = ['a','b','c'];
for (let v of arr.keys()) {
console.log(v);// 0 1 2
}
// set
const arr1 = new Set(['a','b','c']);
for (let v of arr1.keys()) {
console.log(v);//'a' 'b' 'c'
}
// map
const arr2 = new Map();
arr2.set('a','a');
arr2.set('b','b');
for (let v of arr2.keys()) {
console.log(v);//'a' 'b'
}
// includes
// 判断数组中是否存在钙元素,参数:查找的值、起始位置
var a = [1,2,3];
console.log(a.includes(2));//true
console.log(a.includes(4));//false