假设我有一个对象数组,每个对象包含一个嵌套的对象数组likes
:
[
{
id: 1,
title: 'Lorem',
private: false,
likes: [
{ id: 01, username: 'mike' },
{ id: 02, username: 'john' }
]
},
{
id: 2,
title: 'Ipsum',
private: true,
likes: [
{ id: 03, username: 'steve' },
{ id: 04, username: 'mike' },
{ id: 05, username: 'sara' }
]
},
{
id: 3,
title: 'Dolor',
private: false,
likes: [
{ id: 06, username: 'brad' },
{ id: 07, username: 'cameron' },
{ id: 08, username: 'liam' }
]
}
]
如何通过项嵌套数组中的用户名值过滤外部数组?假设要输出包含在其内部数组中的具有属性username: 'mike'
的对象的项目?
答案 0 :(得分:0)
您可以通过查看嵌套数组进行过滤。
var data = [{ id: 1, title: 'Lorem', private: false, likes: [{ id: 01, username: 'mike' }, { id: 02, username: 'john' }] }, { id: 2, title: 'Ipsum', private: true, likes: [{ id: 03, username: 'steve' }, { id: 04, username: 'mike' }, { id: 05, username: 'sara' }] }, { id: 3, title: 'Dolor', private: false, likes: [{ id: 06, username: 'brad' }, { id: 07, username: 'cameron' }, { id: 08, username: 'liam' }] }],
result = data.filter(({ likes }) => likes.some(({ username }) => username === 'mike'));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
使用过滤器和一些功能:
const input = [{
id: 1,
title: "Lorem",
private: false,
likes: [{
id: 1,
username: "mike"
}, {
id: 2,
username: "john"
}]
},
{
id: 2,
title: "Ipsum",
private: true,
likes: [{
id: 3,
username: "steve"
},
{
id: 4,
username: "mike"
},
{
id: 5,
username: "sara"
}
]
},
{
id: 3,
title: "Dolor",
private: false,
likes: [{
id: 6,
username: "brad"
},
{
id: 7,
username: "cameron"
},
{
id: 8,
username: "liam"
}
]
}
];
const criteria = {
username: "mike"
};
const result = input.filter(x =>
x.likes.some(y => y.username === criteria.username)
);
console.log(result);
答案 2 :(得分:0)
您可以使用“地图”功能过滤内部数组
const data = [
{
id: 1,
title: 'Lorem',
private: false,
likes: [
{ id: 1, username: 'mike' },
{ id: 2, username: 'john' }
]
},
{
id: 2,
title: 'Ipsum',
private: true,
likes: [
{ id: 3, username: 'steve' },
{ id: 4, username: 'mike' },
{ id: 5, username: 'sara' }
]
},
{
id: 3,
title: 'Dolor',
private: false,
likes: [
{ id: 6, username: 'brad' },
{ id: 7, username: 'cameron' },
{ id: 8, username: 'liam' }
]
}
]
const res = data.map( item => {
let likes = item.likes.filter( user => user.username === 'mike')
if(likes.length > 0 ) {
item.likes = likes
return item
}
return null
}).filter(item => item !== null )
console.log(res)
答案 3 :(得分:0)
点赞时使用filter
和find
。
const filter = (data, name) =>
data.filter(({ likes }) => likes.find(({ username }) => username === name));
const data = [
{
id: 1,
title: "Lorem",
private: false,
likes: [
{ id: 01, username: "mike" },
{ id: 02, username: "john" },
],
},
{
id: 2,
title: "Ipsum",
private: true,
likes: [
{ id: 03, username: "steve" },
{ id: 04, username: "mike" },
{ id: 05, username: "sara" },
],
},
{
id: 3,
title: "Dolor",
private: false,
likes: [
{ id: 06, username: "brad" },
{ id: 07, username: "cameron" },
{ id: 08, username: "liam" },
],
},
];
console.log(filter(data, "mike"));
console.log(filter(data, "blah"));