JS 理清数组的方法

2020-03-07  本文已影响0人  行走的蛋白质

方法

方法 Array.from()
// 从 String 生成数组
console.log((Array.from('protein'))) // ["p", "r", "o", "t", "e", "i", "n"]

// 从 Set 生成数组
const set1 = new Set(['protein', 'potato', 'tomato'])
console.log(Array.from(set1)) // [ 'protein', 'potato', 'tomato' ]

// 从 Map 生成数组
const fruits = new Map([['apple', 2], ['banana', 3]])
console.log(Array.from(fruits)) // [ [ 'apple', 2 ], [ 'banana', 3 ] ]

// 从类数组对象(arguments)生成数组
function fun1() {
    return Array.from(arguments)
}
console.log(fun1(1, 2, 3)) // [ 1, 2, 3 ]

// 函数
// 遍历 [1, 2, 3] 执行回调函数 x + y,两个值为 map 函数的参数即 x 为遍历的每一项值 y 为下标值
console.log(Array.from([1, 2, 3], (x, y) => x + y)) // [ 1, 3, 5 ]
// 遍历 [1, 2, 3] 执行回调函数 x * 2,x 值为 map 函数的参数
console.log(Array.from([1, 2, 3], x => x * 2)) // [ 2, 4, 6 ]

// 数组去重合并
function combine() {
    let arr = [].concat.apply([], arguments)
    return Array.from(new Set(arr))
}
let arr1 = [1, 2, 2, 3]
let arr2 = [2, 3, 3, 3, 4]
console.log(combine(arr1, arr2)) // [ 1, 2, 3, 4 ]
方法 Array.isArray()
if(!Array.isArray) {
    Array.isArray = function(arg) {
        return Object.prototype.toString.call(arg) === '[object Array]'
    }
}
方法 Array.of()
console.log(Array('protein')) // ['protein']
console.log(Array.of('protein')) // ['protein']

console.log(Array(3)) // [ , , ]
console.log(Array.of(3))// [3]


修改器方法

修改器方法 Array.prototype.pop()
let fruits = ['tomato', 'apple', 'pear']
let lastone = fruits.pop()
console.log(lastone) // pear
console.log(fruits) // [ 'tomato', 'apple' ]
修改器方法 Array.prototype.push()
let fruits = ['tomato', 'apple']
let total = fruits.push('pear')
console.log(fruits) // [ 'tomato', 'apple', 'pear' ]
console.log(total) // 3
total = fruits.push('banana', 'orange')
console.log(fruits) // [ 'tomato', 'apple', 'pear', 'banana', 'orange' ]
console.log(total) // 5
修改器方法 Array.prototype.shift()
修改器方法 Array.prototype.unshift()
修改器方法 Array.prototype.reverse()
修改器方法 Array.prototype.sort()
let arr1 = [2, 4, 1, 6, 3, 5, 10, 22, 9]
console.log(arr1.sort()) // [1, 10, 2, 22, 3, 4, 5, 6, 9]
let arr2 = arr1.sort((x, y) => x - y)
console.log(arr2) // [1, 2, 3, 4, 5, 6, 9, 10, 22]
修改器方法 Array.prototype.splice()
let arr2 = ['apple', 'orange']
arr2.splice(1, 0, 'banana')
console.log(arr2) // [ 'apple', 'banana', 'orange' ]
arr2.splice(1, 1) 
console.log(arr2) // [ 'apple', 'orange' ]
arr2.splice(1, 1, 'pear')
console.log(arr2) // [ 'apple', 'pear' ]
// 如果第一个参数是负数则代表 arr.length - Math.abs(start) 的位置
arr2.splice(-2, 1, 'potato')
console.log(arr2) [ 'potato', 'pear' ]


访问方法

访问方法 Array.prototype.concat()
let arr3 = [1, 2, 3]
let arr4 = ['a', 'b', 'c']
let arr5 = arr3.concat(4, arr4, 5)
console.log(arr5) // [1, 2, 3, 4, "a", "b", "c", 5]
访问方法 Array.prototype.includes()
let arr6 = ['a', 'b', 'c']
console.log(arr6.includes('a')) // true
console.log(arr6.includes('a', 1)) // false
console.log(arr6.includes('a', 100)) // false
console.log(arr6.includes('a', -1)) // false
console.log(arr6.includes('a', -100)) // true
访问方法 Array.prototype.join()
let arr7 = ['a', 'b', 'c']
console.log(arr7.join('')) // abc
console.log(arr7.join()) // a,b,c
console.log(arr7.join('-')) // a-b-c
访问方法 Array.prototype.slice()
let arr8 = ['a', 'b', 'c']
let arr9 = arr8.slice(1)
console.log(arr9) // [ 'b', 'c' ]
console.log(arr8) // [ 'a', 'b', 'c' ]
访问方法 Array.prototype.toString()
let arr10 = ['a', 'b', 'c']
console.log(arr10.toString()) // a,b,c
console.log(arr10.join()) // a,b,c
访问方法 Array.prototype.indexOf()
let arr11 = ['a', 'b', 'c']
console.log(arr11.indexOf('a')) // 0
console.log(arr11.indexOf('a', 1)) // -1
console.log(arr11.indexOf('a', 100)) // -1
console.log(arr11.indexOf('a', -1)) // -1
console.log(arr11.indexOf('a', -100)) // 0
访问方法 Array.prototype.lastIndexOf()
let arr12 = ['a', 'b', 'c', 'a']
console.log(arr12.lastIndexOf('a')) // 3
console.log(arr12.lastIndexOf('a', 10)) // 3
console.log(arr12.lastIndexOf('a', 3)) // 3
console.log(arr12.lastIndexOf('a', 2)) // 0
console.log(arr12.lastIndexOf('a', -2)) // 0
console.log(arr12.lastIndexOf('a', -1)) // 3


迭代方法

迭代方法 Array.prototype.forEach()
迭代方法 Array.prototype.every()
let arr13 = [5, 6, 7, 8, 9]
let result13 = arr13.every(o => o > 4)
console.log(result13) // true
let result131 = arr13.every(o => o > 5)
console.log(result131) // false
迭代方法 Array.prototype.some()
let arr14 = [5, 6, 7, 8, 9]
let result14 = arr14.some(o => o > 8)
console.log(result14) // true
let result141 = arr14.some(o => o > 9)
console.log(result141) // false
迭代方法 Array.prototype.filter()
let arr15 = [5, 6, 7, 8, 9]
let result15 = arr15.filter(o => o > 7)
console.log(result15) // [ 8, 9 ]
迭代方法 Array.prototype.map()
let arr16 = [1, 2, 3]
let arr161 = arr16.map(o => o * 2)
console.log(arr161) // [ 2, 4, 6 ]
console.log(arr16) // [ 1, 2, 3 ]
迭代方法 Array.prototype.reduce()
let arr17 = [1, 2, 3]
let result17 = arr17.reduce((x, y) => x + y)
console.log(result17)
let result171 = arr17.reduce((x, y) => x + y, 10)
console.log(result171)
let result172 = arr17.reduce((x, y, i) => {
   console.log(i) // 1, 2
   return x + y
})
let result173 = arr17.reduce((x, y, i) => {
   console.log(i) // 0, 1, 2
   return x + y
}, 10)
let arr = [1, 2, 3, 4]
let sum = arr.reduce((x, y) => x + y)
console.log(sum) // 10
let arr = ['apple', 'orange', 'tomato', 'potato', 'banana', 'apple']
let result = arr.reduce((pre, cur) => {
    if(cur in pre) {
        pre[cur]++
    } else {
        pre[cur] = 1
    }
    return pre
}, {})
console.log(result) // {apple: 2, orange: 1, tomato: 1, potato: 1, banana: 1}
let arr = ['apple', 'orange', 'tomato', 'potato', 'banana', 'apple']
let result = arr.reduce((pre, cur) => {
    if(!pre.includes(cur)) {
        return pre.concat(cur)
    } else {
        return pre
    }
}, [])
console.log(result) // ["apple", "orange", "tomato", "potato", "banana"]
// 2 - 1
let arr = [[1, 2], [3, 4]]
let result = arr.reduce((pre, cur) => {
    return pre.concat(cur)
}, [])
console.log(result) // [1, 2, 3, 4]

// more - 1
let arr = [[1, 2], [3, 4], [5, 6, [7, 8, [9, 10]]]]
let transFun = (arr) => {
    return arr.reduce((pre, cur) => {
        return pre.concat(Array.isArray(cur) ? transFun(cur) : cur)
    }, [])
}
console.log(transFun(arr)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var arr = [{
        name: 'xiaohong',
        age: 10
    }, {
        name: 'xiaohua',
        age: 20
    }, {
        name: 'xiaoli',
        age: 30
    }]
let sum = arr.reduce((pre, cur) => {
    return pre + cur.age
}, 0)
console.log(sum) // 60
迭代方法 Array.prototype.reduceRight()
let arr18 = [1, 2, 3]
let result18 = arr18.reduceRight((x, y) => x + y)
console.log(result18)
let result181 = arr18.reduceRight((x, y) => x + y, 10)
console.log(result181)
let result182 = arr18.reduceRight((x, y, i) => {
    console.log(i) // 1, 0
    return x + y
})
let result183 = arr18.reduceRight((x, y, i) => {
    console.log(i) // 2, 1, 0
    return x + y
}, 10)
上一篇下一篇

猜你喜欢

热点阅读