25array4.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> // forEach() var arr = [11,3,56,56,34,100,9,100]; /* arr.forEach(function(item,index) { console.log(index+"-"+item); }); */ arr.forEach((item,index)=>{ //console.log(index+"-"+item); if(item%2===0) { console.log(item); } }); // 求上述数组最大值 var max = arr[0]; arr.forEach((item)=>{ if(item>max) { max = item; } }); console.log(max); // indexOf() 查找元素在数组中第一次出现的索引 假如不存在返回值-1 var index = arr.indexOf(200); var index2 = arr.lastIndexOf(100); //从右往左找 console.log(index);//4 console.log(index2); // 6 // 对数组进行去重 function noRepeat(arr) { var newArr = []; // 对arr循环 每一次拿到元素 判断是否在newArr中 没有就加到newArr arr.forEach((item)=>{ if(newArr.indexOf(item) === -1) { newArr.push(item); } }); return newArr; } console.log(noRepeat(arr)); // map filter reduce some every 不会改变原数组 var array = [12,14,5,6,32,10]; /*var newArr = array.map((item)=>{ return item*1.2; }); // filter过滤 由函数返回值为true的元素所组成 var newArr = array.filter((item)=>{ return item>12; }); */ // reduce 收敛 /* 第一次 prev 12 next 14 第二次 prev undefined next 5 第三次 prev undefined next 6 ... 从第二次开始的prev 是上一次的函数返回值 */ var sum = array.reduce((prev,next)=>{ console.log(prev+"---"+next); return prev+next; }); // 当reduce传入第二个参数 第一次的prev指向第二个参数 var sum2 = array.reduce((prev,next)=>{ console.log(prev+"---"+next); return prev+next; },6); console.log(sum2); //console.log(newArr,array); // some 返回true/false var arr2 = [10,4,5,6,11,20]; // var res = arr2.some((item)=>{ console.log(item); return item>100; }); console.log(res); // true console.log("--------------"); // every 直到第一个为false停止循环 否则返回true var res2 = arr2.every((item)=>{ console.log(item); return item>10; }); console.log(res2); </script> </body> </html>
26array5.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> // Math.pow() // es6新增的 from 把数组或类数组变成数组 会复制一份 var arr = [1,2,3]; var newArr = Array.from(arr); console.log(newArr); // 类数组 并不是真正的数组 而是对象 arguments function fn() { //arguments.push(10); 报错 var arguments = Array.from(arguments); arguments.push(12); console.log(arguments instanceof Array); } fn(1,2,3); //console.log(arr instanceof Array); var obj = { "0": 10, "1":20, length: 2 } var arr2 = Array.from(obj); console.log(arr2); // of fill copyWithin var array = new Array(3); // var array = [3]; var array1 = new Array(3,4); var array2 = Array.of(3,4);// 3是元素 console.log(array,array.length); console.log(array1,array1.length); console.log(array2,array2.length); // fill copyWithin var res = [1,2,3,4].fill(5,1,3); // [1,5,5,4] var res2 = [1,2,3,4].copyWithin(1,0,2); // 用序号为0的元素一直到序号为2但不包含2的元素 1 2 console.log(res); console.log(res2);// [1,1,2,4] // es7 includes console.log([10,12,23,43].includes(100));// false 不存在 true存在 // find findIndex (keys values) var find = [1,2,3,4,5,3].find((item,index,arr)=>{ console.log("fn"); return item==13; }); var findIndex = [1,2,3,4,5,3].findIndex((item,index,arr)=>{ return item==3; }); console.log(findIndex); </script> </body> </html>
27array6.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> // 解构赋值 解析数组或对象的结构为不同的变量提取值 //var name = "along"; //var age = 32; var [name,age] = ["along",32]; console.log(name); console.log(age); //var [x,y,z] = [1,[2.2,2.3]]; //var [x,[y,z]] = [1,[2.2,2.3]]; //console.log(x,y,z); // 1 [2.2,2.3] undefined var [json,arr,num] = [{a:10,b:20},[1,2,3],10]; var [,,x=4] = [1,2]; console.log(x); var [x,...y] = [10,20,30,40];// 剩余项 console.log(x,y);// 10 [20,30,40] var arr = [1,2,3]; var [...arr1] = arr; // 克隆数组 console.log(arr1); var x = 10, y = 20; /* var temp = x; x = y; y = temp; */ [x,y] = [y,x]; console.log(x,y); </script> </body> </html>
原文地址:https://www.cnblogs.com/HiJackykun/p/11135520.html
时间: 2024-09-29 21:36:26