let school = [
{
teacher: 'cindy',
student: 'denny',
class: 1
},
{
teacher: 'tom',
student: 'tommey',
class: 2
}
]
console.log(school);
撈資料
console.log(school[1].teacher); 答案:tom
JS array Map
例 ❶
const arr = [1, 5, 50];
const newArr = arr.map(function (item) {
return item * item;
})
例 ❷ (篩選會回傳布林值)
const data = [1, 3, 47, 384];
const newData = data.map(function (item) {
return item > 10;
})
例 ❸
const data = [1, 3, 47, 384];
const newData = data.map(function (item) {
let obj = {};
obj.checkNum = item > 10;
return obj;
})
JS array filter 篩選
例 ❶ (篩選會回傳本身)
const arr = [1, 5, 50];
const newArr = arr.filter(function (item) {
return item >= 5;
})
例 ❷
const data = [
{
name: "小花",
score: 90
}, {
name: "小㓬",
score: 38
}
]
const filterData = data.filter(function (item) {
return item.score >= 60;
})
console.log(filterData);
MAP補充觀念
MAP與forEach差異
陣列操作 II GO