跳至主要内容

[JS30] Day04: Array Cardio Day 1

keywords: Array, 陣列, filter, map, sort, reduce

這天主要是說明幾個在 JS 常用來處理陣列的方法,包含Array.prototype.filter(), Array.prototype.map(), Array.prototype.sort(), Array.prototype.reduce()已統整於另一份筆記中

JavaScript 陣列 @ PJCHENder EverNote

計算陣列中相同元素的數目

回傳物件

let countTransportationObj = {};
data.forEach((item) => {
countTransportationObj[item] = countTransportationObj[item]
? countTransportationObj[item] + 1
: 1;
});

// console.log(countTransportationObj)

回傳包著物件的陣列

let countTransportationArr = [];
for (let prop in countTransportationObj) {
countTransportationArr.push({
name: prop,
count: countTransportationObj[prop],
});
}
// console.log(countTransportationArr)

完成作品

Day04: Array Cardio Day1