1、代码
let persons = [
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 18 },
{ id: 3, name: '王五', age: 16 },
{ id: 4, name: '赵六', age: 40 },
{ id: 5, name: '王二小', age: 35 },
{ id: 6, name: '张小嘎', age: 25 }
];
// 示例一:获取年龄大于20的数据
let filterPersons = persons.filter(person => person.age > 20);
console.log('-----------------------示例一---------------------');
console.log(filterPersons);
// 示例二:获取名字为张小嘎的数据
let filterPersons2 = persons.filter(person => person.name === '张小嘎');
console.log('-----------------------示例二---------------------');
console.log(filterPersons2);
2、输出结果
-----------------------示例一---------------------
[
{ id: 4, name: '赵六', age: 40 },
{ id: 5, name: '王二小', age: 35 },
{ id: 6, name: '张小嘎', age: 25 }
]
-----------------------示例二---------------------
[ { id: 6, name: '张小嘎', age: 25 } ]
转载请注明出处:
未经允许不得转载:lxfamn » js 数组对象过滤结果