数组的几种常用方法
// //unshift()添加,将要给定的数值添加到数组的开头
// var arr1=["王白菜","牛春","野老多"];
// console.log(arr1.unshift("大脸","老安"));//5
// console.log(arr1);
// // ["大脸", "老安", "王白菜", "牛春", "野老多"]
// var arr = [1, 2];
//// arr.unshift(0);
//// arr.unshift(-2, -1); //-2, -1, 0, 1, 2
// arr.unshift([-3]);
// console.log(arr);
//shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
//从数组中删除的元素; 如果数组为空则返回undefined 。
// let a = [1, 2, 3];
// let b = a.shift();
// console.log(a);
// // [2, 3]
// console.log(b);
// // 1
// //push()添加,将要给定的数值添加到数组的后头
// var arr2=["王白菜","牛春","野老多"];
// console.log(arr2.push("大脸","老安"));//5
// console.log(arr2);//"王白菜", "牛春", "野老多", "大脸", "老安"
//pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
//let a = [1, 2, 3];
//a.length; // 3
//a.pop(); // 3
//console.log(a); // [1, 2]
//a.length; // 2
// splice(起点[从1开始],(长度[不删除就为0,删除几个就是几个],元素A,元素B)
var arr=["王白菜","牛春","野老多"];
// arr.splice(1, 0, ‘老安‘);
// console.log(arr);//"王白菜", "老安", "牛春", "野老多"
//想从哪一个后面开始添加就写几
//例如从第一位后开始添加"老安","平板",
// arr.splice(1, 0, "老安","平板");
// console.log(arr);//"王白菜", "老安", "平板", "牛春", "野老多"
// 从第一位后删除牛春,添加"老安","平板",
// arr.splice(1, 1, "老安","平板");
// console.log(arr);//"王白菜", "老安", "平板", "野老多
// arr.splice(1, 0);
// console.log(arr); //"王白菜","牛春","野老多"
//concat()合并,将两个数组合并
var arr1 = [‘a‘, ‘b‘, ‘c‘];
var arr2 = [‘d‘, ‘e‘, ‘f‘];
//=======合并=====
// console.log(arr1.concat(arr2));//["a", "b", "c", "d", "e", "f"]
//========方法一========
// console.log(arr1.concat("j",arr2));//["a", "b", "c", "j", "d", "e", "f"]
// console.log(arr1.concat("j"+arr2));// ["a", "b", "c", "jd,e,f"]
// ===========方法2===========
// console.log(arr1+arr2.concat("j"));//拼接在一起a,b,cd,e,f,j
// //join()将数组(或一个类数组对象)的所有元素变成字符串
// var arr2=["王白菜","牛春","野老多"];
// console.log(arr2.join());//王白菜,牛春,野老多
// console.log(arr2.join(‘‘));//王白菜牛春野老多
// console.log(arr2.join(‘-‘));//王白菜-牛春-野老多
// //reverse()方法是将数组顺序颠倒
// var arr = [‘one‘, ‘two‘, ‘three‘];
// var reversed = arr.reverse();
// console.log(arr);//["three", "two", "one"]
//// console.log(reversed);
// //sort()排序
// var arr = [1, 30, 22, 49, 31, 21];
//// arr.sort();
//// console.log(arr);//[1, 21, 22, 30, 31, 49]
// arr.sort(function(a, b) {
//// return a - b;//升序[1, 21, 22, 30, 31, 49]
// return b - a;//降序[49, 31, 30, 22, 21, 1]
// });
// console.log(arr);
// filter()数组的过滤
// 例子:过滤出数组中单词长度大于6的单词
// let words = [‘spr‘, ‘limit‘, ‘like‘, ‘destruction‘, ‘present‘];
// let result = words.filter(word => word.length > 6);
// console.log(result)
原文地址:https://www.cnblogs.com/yang0629/p/8531042.html