标签:code 定位 存在 values 多个 concat empty range man
1 let arr = [1,2,3,4,5] 2 console.log(arr.push(6,7)) // 7 3 console.log(arr) // [1,2,3,4,5,6,7]
1 let arr = [1,2,3,4,5] 2 console.log(arr.pop()) // 5 3 console.log(arr) // [1,2,3,4]
1 let arr = [1,2,3,4,5] 2 console.log(arr.shift()) // 1 3 console.log(arr) // [2,3,4,5]
1 let arr = [1,2,3,4,5] 2 console.log(arr.unshift(6,7)) // 7 3 console.log(arr) // [6,7,1,2,3,4,5]
参数:index 索引值,num 个数
1 let arr = [1,2,3,4,5] 2 console.log(arr.splice(2,3)) // [3,4] 3 console.log(arr) // [1,2,5]
1 let str = ‘12345‘ 2 console.log(str.split(‘‘)) // ["1","2","3","4","5"]
1 let arr = [1,2,3,4,5] 2 console.log(arr.concat([6,7])) // [1,2,3,4,5,6,7] 3 console.log(arr) // [1,2,3,4,5]
1 let arr = [40,8,10,5,79,3] 2 console.log(arr.sort()) // [10,3,40,5,79,8] 3 4 let arr2 = arr.sort((a,b) => a - b) 5 console.log(arr2) // [3,5,8,10,40,79] 6 7 let arr3 = arr.sort((a,b) => b - a) 8 console.log(arr3) // [79,40,10,8,5,3]
1 let arr = [1,2,3,4,5] 2 console.log(arr.reverse()) // [5,4,3,2,1] 3 console.log(arr) // [5,4,3,2,1]
1 let arr = [1,2,3,4,5] 2 console.log(arr.slice(1,3)) // [2,3] 3 console.log(arr) // [1,2,3,4,5]
1 let arr = [1,2,3,4,5] 2 arr.forEach((value, index, array) => { 3 console.log(`value--${value} index--${index} array--${array}`) 4 }) 5 6 // value--1 index--0 array--1,2,3,4,5 7 // value--2 index--1 array--1,2,3,4,5 8 // value--3 index--2 array--1,2,3,4,5 9 // value--4 index--3 array--1,2,3,4,5 10 // value--5 index--4 array--1,2,3,4,5
1 let arr = [1,2,3,4,5] 2 arr.map( (value, index, array)=>{ 3 value = value * 2 4 }) 5 console.log(arr) // [1,4,6,8,10]
1 let arr = [1,2,3,4,5] 2 let arr2 = arr.filter((value, index) => value >2) 3 console.log(arr2) // [3,4,5]
1 let arr = [1,2,3,4,5] 2 3 let arr2 = arr.every((value, index) => i > 2) 4 console.log(arr2) // false 5 6 let arr3 = arr.every((value, index) => i > 0) 7 console.log(arr3) // true
1 let arr = [1,2,3,4,5] 2 3 let arr2 = arr.some((value, index) => i > 2) 4 console.log(arr2) // true 5 6 let arr3 = arr.some((value, index) => i > 5) 7 console.log(arr3) // false
1 let arr = [1,2,3,4,5,4] 2 3 let arr2 = arr.indexOf(4) 4 console.log(arr2) // 3 5 6 let arr3 = arr.indexOf(6) 7 console.log(arr3) // -1
1 let arr = [1,2,3,4,5,4] 2 3 let arr2 = arr.lastIndexOf(4) 4 console.log(arr2) // 5 5 6 let arr3 = arr.lastIndexOf(6) 7 console.log(arr3) // -1
1 let str = ‘12345‘ 2 console.log(Array.from(str)) // ["1", "2", "3", "4", "5"] 3 4 let obj = {0:‘a‘,1:‘b‘,length:2} 5 console.log(Array.from(obj)) // ["a", "b"]
1 let str = ‘11‘ 2 console.log(Array.of(str)) // [‘11‘] 3 4 等价于 5 console.log(new Array(‘11‘)) // [‘11]
ps:
new Array()有缺点,就是参数问题引起的重载
console.log(new Array(2)) // [empty × 2] 是个空数组
console.log(Array.of(2)) // [2]
1 let arr = [1,2,3,4,5,2,4] 2 let arr2 = arr.find((value, index, array) => value > 2) 3 console.log(arr2) // 3
1 let arr = [1,2,3,4,5] 2 let arr1 = arr.findIndex((value, index, array) => value > 2) 3 console.log(arr1) // 2
let arr = [1,2,3,4,5] let arr2 = arr.includes(2) console.log(arr2) // ture let arr3 = arr.includes(9) console.log(arr3) // false let arr4 = [1,2,3,NaN].includes(NaN) console.log(arr5) // true
参数: target – 待填充的元素
start – 开始填充的位置 - 索引
end – 终止填充的位置 - 索引(不包括该位置)
let arr = [1,2,3,4,5] let arr2 = arr.fill(5) console.log(arr2) // [5, 5, 5, 5, 5] console.log(arr) // [5, 5, 5, 5, 5] let arr3 = arr.fill(5,2) console.log(arr3) // [1,2,5,5,5] let arr4 = arr.fill(5,1,3) console.log(arr4) // [1,5,5,4,5]
1 let arr = [1,2,3,4,5] 2 let arr2 = arr.keys() 3 for (let key of arr2) { 4 console.log(key) // 0,1,2,3,4 5 }
let arr = [1,2,3,4,5] let arr1 = arr.values() for (let val of arr1) { console.log(val); // 1,2,3,4,5 }
1 let arr = [1,2,3,4,5] 2 let arr2 = arr.entries() 3 for (let e of arr2) { 4 console.log(e); // [0,1] [1,2] [2,3] [3,4] [4,5] 5 }
entries() 方法返回迭代数组。
迭代数组中每个值 前一个是索引值作为 key, 数组后一个值作为 value。
参数: target --必选 索引从该位置开始替换数组项
start --可选 索引从该位置开始读取数组项,默认为0.如果为负值,则从右往左读。
end --可选 索引到该位置停止读取的数组项,默认是Array.length,如果是负值,表示倒数
1 let arr = [1,2,3,4,5,6,7] 2 3 let arr2 = arr.copyWithin(1) 4 console.log(arr2) // [1, 1, 2, 3, 4, 5, 6] 5 6 let arr3 = arr.copyWithin(1,2) 7 console.log(arr3) // [1, 3, 4, 5, 6, 7, 7] 8 9 let arr4 = arr.copyWithin(1,2,4) 10 console.log(arr4) // [1, 3, 4, 4, 5, 6, 7]
1 let a = 1234 2 let b = "fsaufh" 3 let c = {a:1,b:2} 4 let d = [1,2] 5 6 let mark1 = Array.isArray(a) 7 console.log(mark1) // false 8 9 let mark2 = Array.isArray(b) 10 console.log(mark2) // false 11 12 let mark3 = Array.isArray(c) 13 console.log(mark3) // false 14 15 let mark4 = Array.isArray(d) 16 console.log(mark4) // true
1 let arr = [1,2,3,4,5] 2 console.log(arr.join()) // 1,2,3,4,5 3 console.log(arr.join("-")) // 1-2-3-4-5
参数:pliy表示拉平的层数,默认是1层,想无限拉平可以传入Infinity关键字
1 let arr = [1, 2, [3, [4, 5]]] 2 console.log(arr.flat(2)) // [1, 2, 3, 4, 5] 3 4 let arr2 = [1,[2,[3,[4,5]]]] 5 console.log(arr2.flat(Infinity)) // [1,2,3,4,5]
1 // 相当于 [[2, 4], [3, 6], [4, 8]].flat() 2 [2, 3, 4].flatMap((x) => [x, x * 2]) 3 // [2, 4, 3, 6, 4, 8]
1 let arr = ["Banana", "Orange", "Apple", "Mango"] 2 console.log(arr.toString()) // Banana,Orange,Apple,Mango
标签:code 定位 存在 values 多个 concat empty range man
原文地址:https://www.cnblogs.com/Faith-Yin/p/12360892.html