使用ES6新数组方法(象C# Lambda表达式一样写查询语句)

let people = [
{id: 1, name: "a", age: 12},
{id: 2, name: "b", age: 13},
{id: 3, name: "c", age: 14},
{id: 4, name: "d", age: 15},
{id: 5, name: "e", age: 16}
]; const kids = [];
people.forEach(person => {
if (person.age < 15) {
kids.push({
id: person.id,
name: person.name
})
}
});
console.log(kids); //map & filter都是数组的方法!
//返回项参与的表达式值
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(number => number * 2); //[2,4,6,8,10]
console.log(doubled); //返回满足条件的项
const even = numbers.filter(number => number % 2 === 0); //[2,4]
console.log(even); //把用forEach示例重写
const kids2 = people
.filter(person => person.age < 15)
.map(person => ({
id: person.id,
name: person.name
}));
console.log(kids2); /*
结论:从上面的示例可知,filter相当于C#的where过滤条件,map相当于C#的select投影
*/

  

上一篇:[游戏开发-学习笔记]菜鸟慢慢飞(九)- NGUI- UIPanel(官方说明翻译)


下一篇:PV与并发之间换算的算法换算公式