有如下数组;请筛选出数组中年龄小年18的对象 var arr = [ {name: '小明', age: 12}, {name: '小红', age: 32}, {name: '小天', age: 45}, {name: '小哈', age: 16}, ]
时间: 2023-04-02 10:00:44 浏览: 140
潜说js对象和数组
可以使用 filter 方法来筛选出年龄小于18的对象:
var arr = [
{name: '小明', age: 12},
{name: '小红', age: 32},
{name: '小天', age: 45},
{name: '小哈', age: 16},
];
var result = arr.filter(function(obj) {
return obj.age < 18;
});
console.log(result); // [{name: '小明', age: 12}, {name: '小哈', age: 16}]
阅读全文