MDN-Array的属性/方法:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array
clear();
//0.数组长度 array.length
console.log("array.length:",[45,67].length); //1.数组-原型 Array.prototype
console.log("Array.prototype:",Array.prototype) //2.Array.from() :对伪数组或可迭代对象(包括arguments Array,Map,Set,String...)转换成数组对象
console.log("Array.from(obj)",Array.from(["a3","56757","56757",[67,8768]]));
console.log("Array.from(obj)",Array.from("johnny")); //3.Array.isArray() 用于确定传递的值是否是一个 Array
console.log("Array.isArray(xxx):",Array.isArray([]),Array.isArray("")) var arr = [3,4,64,4,5,-45,6,67,-65,456435,4354]; //相当好用
//4.数组-过滤器
arr.filter(function(element,index,array){
if(element < 0){
return false;
} else
return true;
}) //相当好用
//5.数组-遍历/处理 Array.map(function(element,index,array){})
arr.map(function(element,index,array){
if(element>100){
console.log(element)
}
return element;
}) //6.数组-排序/处理 Array.sort(compareFunction)
var arrObj = [23,31,45,56];
arrObj.sort(function(a,b){ return a-b; }) //升序:[23,31,45,56] //7.数组元素修改操作:替换/剪切/删除 Array.prototype.splice() 实现数组的灵活删除与插入
var myFish = ["angel", "clown", "mandarin", "surgeon"];
//从第 2 位开始删除 0 个元素,插入 "drum"
var removed = myFish.splice(2, 0, "drum");
//运算后的 myFish:["angel", "clown", "drum", "mandarin", "surgeon"]
//被删除元素数组:[],没有元素被删除 //从第 3 位开始删除 1 个元素
removed = myFish.splice(3, 1);
//运算后的myFish:["angel", "clown", "drum", "surgeon"]
//被删除元素数组:["mandarin"] //从第 2 位开始删除 1 个元素,然后插入 "trumpet"
removed = myFish.splice(2, 1, "trumpet");
//运算后的myFish: ["angel", "clown", "trumpet", "surgeon"]
//被删除元素数组:["drum"] //从第 0 位开始删除 2 个元素,然后插入 "parrot", "anemone" 和 "blue"
removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
//运算后的myFish:["parrot", "anemone", "blue", "trumpet", "surgeon"]
//被删除元素的数组:["angel", "clown"] //从第 3 位开始删除 2 个元素
removed = myFish.splice(3, Number.MAX_VALUE);
//运算后的myFish: ["parrot", "anemone", "blue"]
//被删除元素的数组:["trumpet", "surgeon"]
输出
array.length: 1
05:17:04.718 main.raven.938767835e3e5ae6e744.js:1 Array.prototype: [constructor: ƒ, concat: ƒ, pop: ƒ, push: ƒ, shift: ƒ, …]
05:17:04.721 main.raven.938767835e3e5ae6e744.js:1 Array.from(obj) (4) ["a3", "56757", "56757", Array(2)]
05:17:04.722 main.raven.938767835e3e5ae6e744.js:1 Array.from(obj) (6) ["j", "o", "h", "n", "n", "y"]
05:17:04.722 main.raven.938767835e3e5ae6e744.js:1 Array.isArray(xxx): true false
05:17:04.723 main.raven.938767835e3e5ae6e744.js:1 456435
05:17:04.723 main.raven.938767835e3e5ae6e744.js:1 4354
05:17:04.723 (11) [3, 4, 64, 4, 5, -45, 6, 67, -65, 456435, 4354]
参考文献