1.document.getElementById 得到的是單個(gè)元素
2.document.getElementByClassName得到的是數(shù)組
原生object轉(zhuǎn)array:
Object.values(object)
對(duì)某個(gè)字段進(jìn)行累加:
const totalSum = mapData.reduce(function (total, currentValue, currentIndex, arr) {
return total + currentValue.count;
}, 0);
字段名.reduce((x,y) => x + y , 0)
截取當(dāng)前天數(shù)前幾天的日期
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);
});
匹配倆個(gè)數(shù)組不一樣的值
getArrDifference(arr1, arr2) {
return arr1.concat(arr2).filter(function(v, i, arr) {
return arr.indexOf(v) === arr.lastIndexOf(v);
});
}
數(shù)組去重
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}
數(shù)組json根據(jù)某一個(gè)字段進(jìn)行排序
用到lodash.js
image.png
對(duì)象數(shù)組根據(jù)某個(gè)重復(fù)的字段對(duì)字段數(shù)據(jù)進(jìn)行累加
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], //根據(jù)‘-’截取相同月份
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
}, [])