SOURCE

// 数组中的方法
//join方法,将数组元素组合成立字符串
var arr = [1,2,3,4,5];
console.log(arr)// [1,2,3,4,5]
console.log(arr.join(",")); // 1,2,3,4,5
console.log("=====================================================")
var arr1 = ["张三","李四"];
arr1.push("王五","赵六");
console.log(arr1); // ["张三", "李四", "王五", "赵六"]
var item = arr1.pop();
console.log(item); // 赵六
console.log(arr1); // ["张三", "李四", "王五"]
console.log("=====================================================")
var arr2 = ["张三","李四","王五"];
arr2.unshift("赵六");
console.log(arr2); //  ["赵六","张三","李四","王五"]
var item = arr2.shift();
console.log(item); // 赵六
console.log(arr2); // ["张三", "李四", "王五"]
console.log("=====================================================")
var arr = [1,3,5,7,9,11];
var arrRes1 = arr.splice(0,2);
//删除0开始两个元素
console.log(arr);         // [5, 7, 9, 11]
console.log(arrRes1);     // [1, 3]
var arrRes2 = arr.splice(2,0,4,6);
// 从第二个位置开始插入4和6
console.log(arr);         // [5, 7, 4, 6, 9, 11]
console.log(arrRes2);     // []
 //在第一个位置删除一个元素,并且插入2和4
var arrRes3 = arr.splice(1,1,2,4);
console.log(arr);         // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRes3);     // [7]
console.log("=====================================================")
// 传入负数的时候,要先加上是字符串或者数组的长度
var arr = [1,3,5,7,9,11];
var arrRes1 = arr.slice(1);
var arrRes2 = arr.slice(1,4);
var arrRes3 = arr.slice(1,-2);
var arrRes4 = arr.slice(-4,-1);
console.log(arrRes1);  // [3, 5, 7, 9, 11]
console.log(arrRes2);  // [3, 5, 7]
console.log(arrRes3);  // [3, 5, 7]
console.log(arrRes4);  // [5, 7, 9]
console.log("=====================================================")
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// index代表的是下标,而i代表的是数组中的元素
var arrRes = arr.filter(function(i, index) {
    return index % 3 === 0 || i >= 8;
});
console.log(arrRes ); //[1, 4, 7, 8, 9, 10]
console.log("=====================================================")
var arr1 = [1, 3, 5, 7];
var arr2 = [2, 4, 6, 8];
console.log(arr1.concat(arr2)) //[1, 3, 5, 7, 2, 3, 6 ,8]
console.log("=====================================================")
var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5));       //2
console.log(arr.lastIndexOf(5));   //5
console.log(arr.indexOf(5,2));     //2
console.log(arr.lastIndexOf(5,4)); //2
console.log(arr.indexOf("5"));     //-1
console.log("=====================================================")
// every
var arr = [1, 2, 3, 4, 5];
var arrRes1 = arr.every(function(i) {
    return i < 10;
});
console.log(arrRes1); // true
 
var arrRes2 = arr.every(function(i) {
    return i < 3;
});
console.log(arrRes2); // false
console.log("=====================================================")
// some
var arr = [1, 2, 3, 4, 5];
var arrRes1 = arr.some(function(i) {
    return i < 3;
});
console.log(arrRes1); // true
var arrRes2 = arr.some(function(i) {
    return i < 1;
});
console.log(arrRes2); // false
console.log("=====================================================")
// includes
var arr1 = [1, 2, 3, 4];
    console.log(arr1.includes(1));//输出 true
    console.log(arr1.includes(10))//输出 false
console.log("=====================================================")
// sort
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.sort());  //Apple,Banana,Mango,Orange
var arr = [12, 1, 2, 21, 3];
console.log(arr.sort());  //得到的结果为 1,12,2,21,3

var arr1 = [5, 2, 1, 9, 10];
arr1.sort(function (a,b) {//参数是一个回调函数,且回调函数得参数是2个。
     return a - b;  //如果是a-b,那么就是升序排列,如果是b-a就是降序排列。
})
console.log(arr1);//输出 [ 1, 2, 5, 9, 10 ]

var arr2 = ["a", "bb", "ccc", "dddd"];
 arr2.sort(function (a, b) {
    return b.length - a.length; //输出 [ 'dddd', 'ccc', 'bb', 'a' ]
})
console.log(arr2);
console.log("=====================================================")
// 数组翻转
var arr1 = [1, 2, 3];
arr1.reverse()
console.log(arr1);//输出 [3,2,1]
console.log("=====================================================")
// forEach
var arr = [1, 2, 3, 4, 5];
arr.forEach(function(i, index){
    console.log(i + '|' + index);
});
// 输出为:
// 1|0
// 2|1
// 3|2
// 4|3
// 5|4
console.log("=====================================================")
// map
var arr = [1, 2, 3, 4, 5];
var arrRes = arr.map(function(item){
    return item*item;
});
console.log(arrRes ); //[1, 4, 9, 16, 25]
console.log("=====================================================")
var arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3)
console.log(arr)
var arr1 = [1, 2, 3, 4, 5];
arr1.copyWithin(0, -2, -1)
console.log(arr1)
console.log("=====================================================")
var arr1 = [1, 2, 3, 4, 5];
var num = arr1.find(item =>item>1)
console.log(num)//2
console.log("=====================================================")
var arr = ['a','b','c'].findIndex(item =>{
    return item === 'b'
})
console.log(arr);//1
console.log("=====================================================")
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
  console.log(prev, cur, index);
  return prev + cur;
})
console.log(arr, sum);
console 命令行工具 X clear

                    
>
console