前端处理数据方法

194 阅读4分钟
  1. 数组方法总结

image.png

  • 改变原始数组

    • 数组.push(数据) 从后面追加;返回值:追加后数组最新的长度 增(尾)
    • 数组.pop() 从后面删除(最后一个数据);返回值:被删除的数据 删(尾)
    • 数组.unshift(数据) 从前面添加;返回值:添加后数组最新的长度 增(头)
    • 数组.shift() 从前面删除(最前一个数据);返回值:被删除的数据 删(头)
    • 数组.splice(开始索引,几个元素) 删除指定位置的几个元素 删(任意位置)
    • 数组.splice(开始索引,0, a,b,c) 将a,b,c插入到指定位置 插(指定位置)
    • 数组.splice(开始索引, 3, "a", "b", "c") 将"a","b","c"3个元素替换掉,修改指定位置的元素 改(指定位置)
    • 数组.reverse() 反转数组;返回值:被反转后的数组
    • (截取并添加)数组.splice(开始索引,多少个,要插入的数据),开始索引:默认0; 多少个:默认0; 要插入的数据:默认没有。删除数组中若干数据,并选择是否插入新的数据(从哪里删除从哪里插入);返回值:以新数组的形式返回被删除的数据
    • 数组.sort() 数组排序;数组.sort(function(a,b){return a-b}) 升序; 数组.sort(function(a,b){return b-a})降序;返回值:排序好的数组
  • 不改变原始数组

    • 数组.join(连接符) 将数组用连接符连接成一个字符串;返回值:连接好的字符串
    • 数组.concat(其他数组) 将其他数组和数组拼接在一起;返回值:拼接好的新数组arr1.concat(arr2)。也可以直接加(arr1+arr2)
    • 数组.slice(开始索引,结束索引),开始索引默认是0,结束索引默认是数组长度; 截取数组中的某些数据(包前不包后);返回值:以新数组的形式返回截取出来的数据
    • 数组.indexOf(数据) 查找数据在数组中的索引位置;返回值:有该数据,返回第一次出现的索引位置。没有该数据,返回-1
    • 数组.forEach(function(item, index, arr){}) 遍历数组;返回值:无 快速迭代,没有返回值
        var names = ["abc", "nba", "cctv", "av"]
        names.forEach(function(t) {
            alert(t)
        })
    
    • 数组.map(function(item, index, arr){}) 映射数组;返回值:映射后的数组 遍历数组中每一个元素传入到函数中。元素会经过函数中的指令进行各种变换,生成新的元素,并且将新的元素返回
    // 在names中所有元素后面拼接-abc
    var names = ["abc", "nba", "cctv", "av"]
    var flag = names.map(function(t) {
       return t + "abc"
    })
    
    • 数组.filter(function(item, index, arr){}) 过滤数组;返回值:过滤后的新数组 遍历数组中每一个元素传入到函数中。函数的结果返回true,那么这个元素会被添加到新的数组中;返回false,则忽略该元素
    // 获取names中所有包含'a'字符的元素
    var names = ["abc", "nba", "cctv", "av"]
    var flag = names.filter(function(t) {
       return t.indexOf('a') != -1
    })
    
    • 数组.every(function(item, index, arr){}) 判断是否每一项都满足条件;返回值:一个布尔值 迭代(将数组中的每一个元素都传入到一个函数中,该函数返回true/false)
    // 判断一组元素中是否都包含一个字符
    var names = ["abc", "nba", "cctv", "av"]
    var flag = names.every(function(t) {
        return t.indexOf('a') != -1
    })
    
    • 数组.some(function(item, index, arr){}) 判断是否有某一项满足条件;返回值:一个布尔值 迭代(将数组中的每一个元素都传入到一个函数中,该函数返回true/false)

      // 判断一组元素中是否含有某一个字符
      var names = ["abc", "nba", "cctv", "av"]
      var flag = names.some(function(t) {
         return t.indexOf('a') != -1
      })
      
    • 数组.reduce(callback[, initialValue])

      • 参数 previousValue(上一次调用回调函数时的返回值,或者初始值),默认第一次为0
      • 参数 currentValue(当前正在处理的数组元素)
      • 参数 currentIndex(当前正在处理的数组元素下标)
      • 参数 array(调用reduce方法的数组)
        // 求一段数字的累加和
        var nums = [1, 2, 3, 4]
        

      // 1.for循环实现 var total = 0 for (var i=0; i<nums.length; i++) { total += nums[i] }

      // 2. forEach实现(相比for,forEach更符合我们的思维,常常用来遍历数组中的元素) var total = 0 nums.forEach(function(t){ total += t })

      // 3. reduce实现 var total = nums.reduce(function(pre, cur){ return pre + cur }) ```

  1. 字符串方法总结
  • 都不改变原始字符串,都以返回值形式出现
    • 字符串.charAt(索引) 获取对应索引位置的字符;返回值:对应索引位置的字符
    • 字符串.toLowerCase() 转小写;返回值:转换好的字符串
    • 字符串.toUpperCase() 转大写;返回值:转换好的字符串
    • 字符串.replace(换下内容,换上内容) 替换字符串(只能替换第一个);返回值:替换好的字符串
    • 字符串.trim() 去除首尾空格;返回值:去除空格后的字符串
    • 字符串.split(分隔符) 按照分隔符将字符串切割成为一个数组;返回值:切割后的数组
    • 字符串.substr(开始索引,多少个) 截取字符串;返回值:截取出来的字符串
    • 字符串.substring(开始索引,结束索引) 截取字符串(包前不包后);返回值:截取出来的字符串
    • 字符串.slice(开始索引,结束索引) 截取字符串(包前不包后);返回值:截取出来的字符串

3.数字常用方法

  • Math.random() 获取01之间的随机小数,包含0,但是不包含1;返回值:01之间的随机小数
  • Math.round(数组) 对数字进行四舍五入取整;返回值:四舍五入后的整数
  • Math.ceil(数字) 对数字进行向上取整;返回值:向上取整后的整数
  • Math.floor(数字) 对数字进行向下取整;返回值:向下取整后的整数
  • Math.pow(底数,指数) 对数字进行去幂运算;返回值:去幂后的结果
  • Math.sqrt(数字) 对数字进行二次方跟运算;返回值:二次方跟后的结果
  • Math.abs(数字) 对数字进行绝对值运算;返回值:绝对值运算后的结果
  • Math.max(数字1,数字2,数字3,...) 获取若干数字的最大值;返回值:若干个数字中的最大值
  • Math.min(数字1,数字2,数字3,...) 获取若干数字的最小值;返回值:若干个数字中的最小值
  • Math.PI 得到一个近似Π的值 3.json数据结构处理
let stoda = this.ruleForm.singleReports;
const result = stoda.map((item) => {
    const obj = {
        reportProject: item.reportProject,
        reportWorkType: item.reportWorkType,
        reportIssue: item.reportIssue
    }
    obj.invoices = item.invoices.map((it) => {
        invoiceType: it.invoiceType
    })
    return obj
})
console.log(stoda)

4.将对象转换成数组

 const db = {
            aaa: 'msg',
            bbb: {
                abc: 123
            }
        }

        const arr = []
        console.log(Object.entries(db)); //[["aaa","msg"],["bbb",{abc:123}]]
        for (let [k, v] of Object.entries(db)) {
            arr.push({
                id: k,
                value: v
            })
        }
        console.log(arr); // [{id: 'aaa', value: 'msg'},{id: 'bbb', value: {abc: 123}}]

5.ES6数组方法find/findIndex

  const arr = [{
            id: 1,
            name: 'James'
        }, {
            id: 2,
            name: 'Tom'
        }]
        console.log(arr.find(item => item.id === 1)); //{id: 1, name: 'James'}
        console.log(arr.findIndex(item => item.id === 1)) // 0
  1. 判断数组
            let arr = []
            let obj = {}
            console.log(Object.prototype.toString.call(arr)) // [object Array]
            console.log(Object.prototype.toString.call(obj)); // [object Object]

            console.log(arr.constructor === Array); // true
            console.log(obj.constructor === Array); // false

            console.log(arr instanceof Array); // true
            console.log(obj instanceof Array); // false

            console.log(Array.isArray(arr)); // true
            console.log(Array.isArray(obj)); // false
  1. 通过递归扁平化数组(多维数组转化为一维数组)
         const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlatten(v) : v))
        const result = deepFlatten([1, [2, 3],[4, [5], 6], 7])
        console.log(result); // [1,2,3,4,5,6,7]

  1. 求数组的交集与差集
        const a = [0, 1, 2, 3, 4]
        const b = [1, 2, 3, 4, 5]
        const intersection = a.filter(v => b.includes(v))
        console.log(intersection) // [1,2,3,4]

        const difference = a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
        console.log(difference) //[0,5]