项目种常用的js数据处理方法

1.正则处理时间格式(把/用-替换)

  this.conversionListData.map(item => {
                         item.reportDate = item.reportDate.replace(/\//g,"-");
                });

2.split() 方法用于把一个字符串分割成字符串数组

"2:3:4:5".split(":")	//将返回["2", "3", "4", "5"]
"|a|b|c".split("|")	//将返回["", "a", "b", "c"]
"hello".split("")	//可返回 ["h", "e", "l", "l", "o"]
"hello".split("", 3)	//可返回 ["h", "e", "l"]

3.map方法

data.map(item => {
        dailyArray.push(item.activeDate);
        countArray1.push(item.activeCount);
      });

4.forEach()

 echartsData.forEach(data=>{
                keys.push(data.platformName)
                initData.push({
                    name:data.platformName,
                    value:data.activeCount
                })
            })

5.slice()

从已有的数组中返回选定的元素。如果 end 未被规定,那么 slice() 方法会选取从 start 到数组结尾的所有元素。

  var day = ("0" + date.getDate()).slice(-2);
  var month = ("0" + (date.getMonth() + 1)).slice(-2);

6.for..of..

 function getActiveCount(date, map){
          for(let item of map){
            if(item.activeDate === date){
              return item.activeCount
            }
          }
          return 0;
        }
        
        for(let date of dailyArray){
          if(itemDate.android !== undefined){
            countArray1.push(getActiveCount(date,itemDate.android))
          }else{
            countArray1.push(0)
          }
     }

7. join 数组转字符串,split 字符串转数组

8.filter()

var a = [{id: 1}, {id: 2}, {id: 3}]
let b = a.filter(item => item.id > 1);
let b = a.filter(function(item){
    return item.id > 1;
})

9.find()方法返回数组中符合测试函数条件的第一个元素。否则返回undefined

var a = [{id: 1}, {id: 2}, {id: 3}]
a.find(item => item.id == 2);

10.Array.prototype.includes方法返回一个布尔值,属于 ES7 ,indexOf方法,检查是否包含某个值。
 

var a = [1, 2, 3];
if (a.includes(3)) {}
if (a.indexOf(3) !== -1) {}

7.reduce的高级用法
常用的数组求和,求乘积

var  arr = [1, 2, 3, 4];
var sum = arr.reduce((x,y)=>x+y)
var mul = arr.reduce((x,y)=>x*y)
console.log( sum ); //求和,10
console.log( mul ); //求乘积,24

8.对象遍历

  1. for in
  2. Object.keys(a)
  3. Object.values(a)
var a = {id: 1, name: "zhangsan", age: 18};
for(let key in a) {
    console.log(key + ":" + a[key]);
}
// 输出:
// id:1
// name:zhangsan
// age:18

Object.keys(a);
// 输出:["id", "name", "age"]

Object.values(a)
// 输出:[1, "zhangsan", 18]

计算数组中每个元素出现的次数

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];

let nameNum = names.reduce((pre,cur)=>{
  if(cur in pre){
    pre[cur]++
  }else{
    pre[cur] = 1 
  }
  return pre
},{})
console.log(nameNum); //{Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}

 数组去重

new Set()

let arr = [1,2,3,4,4,1]
let newArr = arr.reduce((pre,cur)=>{
    if(!pre.includes(cur)){
      return pre.concat(cur)
    }else{
      return pre
    }
},[])
console.log(newArr);// [1, 2, 3, 4]

 将二维数组转化为一维

array.flat() 

let arr = [[0, 1], [2, 3], [4, 5]]
let newArr = arr.reduce((pre,cur)=>{
    return pre.concat(cur)
},[])
console.log(newArr); // [0, 1, 2, 3, 4, 5]

 将多维数组转化为一维

let arr = [[0, 1], [2, 3], [4,[5,6,7]]]
const newArr = function(arr){
   return arr.reduce((pre,cur)=>pre.concat(Array.isArray(cur)?newArr(cur):cur),[])
}
console.log(newArr(arr)); //[0, 1, 2, 3, 4, 5, 6, 7]

 对象里的属性求和

var result = [
    {
        subject: 'math',
        score: 10
    },
    {
        subject: 'chinese',
        score: 20
    },
    {
        subject: 'english',
        score: 30
    }
];

var sum = result.reduce(function(prev, cur) {
    return cur.score + prev;
}, 0);
console.log(sum) //60
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值