![]() |
体贴的匕首 · objective-c和java下解析对象类 ...· 3 周前 · |
![]() |
风流倜傥的麦片 · boost::property_tree读取 ...· 5 月前 · |
![]() |
礼貌的米饭 · Cannot load driver ...· 6 月前 · |
![]() |
狂野的卤蛋 · 走出国门,管好自己的智能手机 - 中国军网· 7 月前 · |
![]() |
傲视众生的凉面 · Cr渲染器7.1与10.2的区别-西瓜视频· 8 月前 · |
![]() |
非常酷的豆腐
4 月前 |
在JavaScript中,可以使用一些方法来过滤对象子数组和对象子数组。下面是一些常用的方法:
const data = [
{ id: 1, name: 'John', hobbies: ['reading', 'coding'] },
{ id: 2, name: 'Jane', hobbies: ['swimming', 'painting'] },
{ id: 3, name: 'Bob', hobbies: ['coding', 'gaming'] }
const filteredData = data.filter(obj => obj.hobbies.includes('coding'));
console.log(filteredData);
输出结果为:
[
{ id: 1, name: 'John', hobbies: ['reading', 'coding'] },
{ id: 3, name: 'Bob', hobbies: ['coding', 'gaming'] }
]
const data = [
{ id: 1, name: 'John', hobbies: ['reading', 'coding'] },
{ id: 2, name: 'Jane', hobbies: ['swimming', 'painting'] },
{ id: 3, name: 'Bob', hobbies: ['coding', 'gaming'] }
const filteredData = data.map(obj => ({
...obj,
hobbies: obj.hobbies.filter(hobby => hobby !== 'coding')
console.log(filteredData);
输出结果为:
[
{ id: 1, name: 'John', hobbies: ['reading'] },
{ id: 2, name: 'Jane', hobbies: ['swimming', 'painting'] },
{ id: 3, name: 'Bob', hobbies: ['gaming'] }
]
const data = [
{ id: 1, name: 'John', hobbies: ['reading', 'coding'] },
{ id: 2, name: 'Jane', hobbies: ['swimming', 'painting'] },
{ id: 3, name: 'Bob', hobbies: ['coding', 'gaming'] }
const filteredData = data.reduce((acc, obj) => {
const filteredHobbies = obj.hobbies.filter(hobby => hobby !== 'coding');
if (filteredHobbies.length > 0) {
acc.push({ ...obj, hobbies: filteredHobbies });
return acc;
}, []);
console.log(filteredData);
输出结果为:
[
{ id: 1, name: 'John', hobbies: ['reading'] },
{ id: 2, name: 'Jane', hobbies: ['swimming', 'painting'] },
{ id: 3, name: 'Bob', hobbies: ['gaming'] }
]
这些方法可以根据具体的需求来过滤对象子数组和对象子数组中的元素。在实际应用中,可以根据业务逻辑选择合适的方法来实现过滤功能。
腾讯云相关产品和产品介绍链接地址:
请注意,以上仅为腾讯云的一些相关产品,其他云计算品牌商也提供类似的产品和服务。