美文网首页
JavaScript

JavaScript

作者: Jidahan | 来源:发表于2020-04-25 20:36 被阅读0次

1.document.getElementById 得到的是单个元素
2.document.getElementByClassName得到的是数组

原生object转array:

Object.values(object)

对某个字段进行累加:

 const totalSum = mapData.reduce(function (total, currentValue, currentIndex, arr) {
          return total + currentValue.count;
        }, 0);

字段名.reduce((x,y) => x + y , 0)

截取当前天数前几天的日期

var currentDay = new Date().getDay();

    if (currentDay === 0) {

      currentDay = 7; //定义前几天的值

    }

var dates = Array.from({length: currentDay - 1}, (v, k) => k + 1).reverse().map(number => {

    var date = new Date();

    date.setDate(date.getDate() - number);

    return date.toJSON().slice(0, 10);

});

匹配俩个数组不一样的值

getArrDifference(arr1, arr2) {

    return arr1.concat(arr2).filter(function(v, i, arr) {

        return arr.indexOf(v) === arr.lastIndexOf(v);

    });

  }


数组去重

var arr = [1,2,3,3,1,4];
[...new Set(arr)]; // [1, 2, 3, 4]
Array.from(new Set(arr)); // [1, 2, 3, 4]
[...new Set('ababbc')].join(''); // "abc" 字符串去重
new Set('ice doughnut'); //["ice","doughnut"]

并集

var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var union = new Set([...a, ...b]); // {1, 2, 3, 4}

交集

var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var intersect = new Set([...a].filter(x => b.has(x))); // {2, 3}

差集

var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var difference = new Set([...a].filter(x => !b.has(x))); // {1}

数组json根据某一个字段进行排序

用到lodash.js


image.png

对象数组根据某个重复的字段对字段数据进行累加

const data  = [
    { time: '2021-02-24', count: '0' },
    { time: '2021-02-25', count: '57' },
    { time: '2021-02-26', count: '14994' },
    { time: '2021-02-27', count: '0' },
    { time: '2021-02-28', count: '5' },
    { time: '2021-03-01', count: '0' },
    { time: '2021-03-02', count: '0' },
    { time: '2021-03-03', count: '0' },
    { time: '2021-03-04', count: '0' },
    { time: '2021-03-05', count: '0' },
    { time: '2021-03-06', count: '0' },
    { time: '2021-03-07', count: '0' },
    { time: '2021-03-08', count: '0' },
    { time: '2021-03-09', count: '0' },
    { time: '2021-03-10', count: '0' },
    { time: '2021-03-11', count: '0' },
    { time: '2021-03-12', count: '1' },
    { time: '2021-03-12', count: '0' },
    { time: '2021-03-13', count: '0' },
    { time: '2021-04-1', count: '0' },
    { time: '2021-04-2', count: '0' },
    { time: '2021-04-3', count: '5' },
    { time: '2021-04-4', count: '1' },
    { time: '2021-12-4', count: '1' },
    { time: '2021-11-4', count: '1' },
    { time: '2021-11-30', count: '1' },
  ]
  let oneData = []

  const tdata = data.map(reward => { 
    return {
      time : reward.time.split('-')[1],   //根据‘-’截取相同月份
      count: Number(reward.count)
    }
  })
  const arr1 = tdata.reduce((total, cur, index) => {
    let hasValue = total.findIndex(current => { return current.time === cur.time})
    hasValue === -1 && total.push(cur)
    hasValue !== -1 && (total[hasValue].count = total[hasValue].count + cur.count)
    return total
  }, [])

相关文章

网友评论

      本文标题:JavaScript

      本文链接:https://www.haomeiwen.com/subject/dcclwhtx.html