【数组方法】
目录
let sum=0
const ss=[1,2,3,4,5,6]
//抽离方法
function test (s) {
sum+=s
console.log(s)
}
ss.forEach(test)
console.log("sum",sum)
//todo 练习2 迭代push()方法的替换方案
let nums =ss.map(function (s) {
return s
})
console.log("nums",nums)
//todo 练习3 收集对象数组中的某一个值
let dd=[
{a:'aa',b:'bb'},
{a:'aaa',b:'bbb'}
]
let cars =dd.map(function (car) {
return car.b
})
console.log("cars",cars)
filter方法
//todo 练习3 过滤掉非id==1的学生
let obj={id:1,name:'张三'};
let objArr=[
{sid:1,name:'zhangsan'},
{sid:2,name:'lisi'},
{sid:3,name:'wangwu'},
{sid:4,name:'zhaoliu'},
{sid:5,name:'songba'},];
//ES6 语法
function filterTest(obj,objArr){
return objArr.filter(function (arr) {
return obj.id===arr.sid;
})
}
console.log("这人是谁:",filterTest(obj,objArr))