<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>高阶函数</title>
<style>
table {
border-collapse: collapse;
}
thead {
background-color: #ccc;
}
td {
padding: 5px 15px;
border: 1px solid gray;
}
.yi {
background-color: #eee;
}
.wei {
background-color: #abc;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>薪资</td>
<td>婚姻状态</td>
</tr>
</thead>
<tbody id="box">
<!-- <tr>
<td>1</td>
<td>凯凯1</td>
<td>23</td>
<td>7000</td>
<td>已婚</td>
</tr> -->
</tbody>
</table>
<script>
var emps = [
{ ename: "凯凯1", age: 23, salary: 7000, married: true },
{ ename: "凯凯2", age: 33, salary: 17000, married: true },
{ ename: "凯凯3", age: 44, salary: 9000, married: false },
{ ename: "凯凯4", age: 28, salary: 12000, married: true },
{ ename: "凯凯5", age: 35, salary: 33000, married: false },
]
// 判断是否所有人都大于30岁
var a = emps.every(e => e.age > 30)
console.log(a ? '都大于30' : '非都大于30');
// 判断是否有人工资超过3w
var a = emps.some(e => e.salary > 30000)
console.log(a ? '有人过3w' : '没有过3W')
// 找出所有已婚的人
var a = emps.filter(e => e.married)
console.log(a)
// 数组元素显示在表格里, 已婚和未婚 添加不同的背景色
var a = emps.map((value, index) => {
return `<tr class="${value.married ? 'yi' : 'wei'}">
<td>${index + 1}</td>
<td>${value.ename}</td>
<td>${value.age}</td>
<td>${value.salary}</td>
<td>${value.married ? '已婚' : '未婚'}</td>
</tr>`
})
box.innerHTML = a.join('')
// 把数组中每个人的薪资 +1000
emps.forEach(e => e.salary += 1000)
console.log(emps)
// 统计年龄超过30岁的人 的年薪总和
var a = emps.reduce((sum, value) => {
if (value.age > 30) {
return sum + value.salary * 12
} else {
return sum
}
}, 0)
console.log(a)
</script>
</body>
</html>