js 数组相关

191 阅读3分钟

相关知识点

Array.filter()

array.filter(function(currentValue,index,arr), thisValue)

​ 创建一个新的数组,返回回调函数中判断为true的元素

  1. 不会对空数组进行过滤
  2. 不会改变原数组

用于过滤,查找某一条件元素

Array.map()

array.map(function(currentValue,index,arr), thisValue)

​ 返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

  1. 不会对空数组进行过滤
  2. 不会改变原数组

用于映射,根据条件对数组元素进行映射

Array.reduce()

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

​ reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

  1. reduce() 对于空数组是不会执行回调函数的。

filter、map的组合

Array.every()

array.every(function(currentValue,index,arr), thisValue)

every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。

every() 方法使用指定函数检测数组中的所有元素:

  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。

注意: every() 不会对空数组进行检测。

注意: every() 不会改变原始数组。

find()

array.find(function(currentValue, index, arr),thisValue)

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

find() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数
  • 如果没有符合条件的元素返回 undefined

注意: find() 对于空数组,函数是不会执行的。

注意: find() 并没有改变数组的原始值

findIndex()

array.findIndex(function(currentValue, index, arr), thisValue)

findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

findIndex() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 -1

注意: findIndex() 对于空数组,函数是不会执行的。

注意: findIndex() 并没有改变数组的原始值。

数组去重

let arr = [1, 2, 3, '1', 2, 3, 4, 5, {}, {}, '{}', '{}', null, null, 'null', undefined, undefined, NaN, NaN, true, true, 'true']

Object.hasOwnProprety()

let unique = arr => {
    let mapObj = {}
    // 这里也可以用filter
    //return arr.filter(item=>{
    //    return mapObj.hasOwnProprety(typeof item + item) ? false : (mapObj[typeof item + item]=true)
    //})
    return arr.reduce((ls,item)=>{
         return mapObj.hasOwnProperty(typeof item + item)
         		? ls 
         		: (mapObj[typeof item + item] = true, ls.concat(item))
    },[])
}
console.log(unique(arr))  
// [1, 2, 3, '1', 4, 5, {}, '{}', null, 'null', undefined, NaN, true, 'true'] 所有都可以实现

Set()

let unique_set = arr => [...new Set(arr)]
console.log(unique_set(arr)) 
// [1, 2, 3,'1', 4, 5,{}, {}, '{}',null, 'null', undefined,NaN, true, 'true'] 空对象无法去重

排序

冒泡排序

较简单的排序算法

原理

需要遍历 length - 1 次 每一次遍历 都从后往前进行比较, 相邻的两两比较大小,大的向后浮动 时间复杂度 O(n^2^)

实现

// 冒泡排序
function dubbleSort(arr) {
    let len = arr.length
    let flag = true
    for (let i = 0; i < len && flag; i++) {
        flag = false
        for (let j = 1; j < len - i; j++) {
            if (arr[j] < arr[j - 1]) {
                [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]] // ES6结构赋值
                flag = true
                // let temp = arr[j]  // 临时变量
                // arr[j] = arr[j - 1]
                // arr[j - 1] = temp
            }
        }
    }
    return arr
}
let sortArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
console.log(dubbleSort(sortArr))
// [
//   1,  2,  3,  4,  5,  6,  7,
//   8,  9, 10, 11, 12, 13, 14,
//  15, 16, 17, 18, 19, 20
// ]

快速排序

原理

选定一个基准值,数组其他元素跟基准值对比,分成左右两个数组,再递归调用排序算法

实现

待续