gpt4 book ai didi

javascript - 普通 JavaScript : Filter nested array by array

转载 作者:行者123 更新时间:2023-12-02 21:10:06 24 4
gpt4 key购买 nike

考虑以下帖子数组:

posts_array =
[
{
id: 1,
categories: [
{
category_id: 1,
category_name: "one"
},
{
category_id: 2,
category_name: "two"
},
},
{
id: 2,
categories: [
{
category_id: 1,
category_name: "one"
},
{
category_id: 3,
category_name: "three"
},
},
{
id: 3,
categories: [
{
category_id: 1,
category_name: "one"
},
{
category_id: 4,
category_name: "four"
},
{
category_id: 5,
category_name: "five"
},
}
]

然后我有一个类别 id 数组,我想用它来过滤上面的数组:

const category_id_array = [1, 4];

我想进行过滤以满足所有条件,即返回 posts_array 中包含两者categories.category_id = 1 categories.category_id = 4的元素

我尝试过以下内容(使用 .some 函数),但它返回满足任一条件的元素,即任一categories.category_id = 1类别。类别 ID = 4

function filterPostsByCategoryIds(category_id_array){
const filter_categories = category_id_array;
const custom_filter = d => d.categories.some(c => filter_categories.includes(c.category_id));
const filtered_posts_array = posts_array.filter(custom_filter);
return filtered_posts_array;
}

用 .every 交换 .some 根本不会返回任何元素。

如何通过category_id_array过滤posts_array,以便它返回上面示例数组中的最后一个元素,即类别包含category_id_array中的所有category_id?

谢谢,注意安全!

最佳答案

您可以使用.every()在您的category_id_array上并返回true当有.some()时当前.categories内的对象其中有 category_id等于 c 中的当前 id ( category_id_array ) :

const posts_array = [{ id: 1, categories: [{ category_id: 1, category_name: "one" }, { category_id: 2, category_name: "two" } ], }, { id: 2, categories: [{ category_id: 1, category_name: "one" }, { category_id: 3, category_name: "three" } ], }, { id: 3, categories: [{ category_id: 1, category_name: "one" }, { category_id: 4, category_name: "four" }, { category_id: 5, category_name: "five" } ], }];

function filterPostsByCategoryIds(category_id_array) {
const custom_filter = d => category_id_array.every(c => d.categories.some(o => o.category_id === c));
const filtered_posts_array = posts_array.filter(custom_filter);
return filtered_posts_array;
}

const res = filterPostsByCategoryIds([1, 4]);
console.log(res);

您还可以创建一组 category_id对于每个对象,然后使用 .every().has()检查类别数组是否具有 category_id 中的每个 id像这样:

const posts_array = [{ id: 1, categories: [{ category_id: 1, category_name: "one" }, { category_id: 2, category_name: "two" } ], }, { id: 2, categories: [{ category_id: 1, category_name: "one" }, { category_id: 3, category_name: "three" } ], }, { id: 3, categories: [{ category_id: 1, category_name: "one" }, { category_id: 4, category_name: "four" }, { category_id: 5, category_name: "five" } ], }];

function filterPostsByCategoryIds(category_id_array) {
return posts_array.filter(({categories}) => {
const cat_ids = new Set(categories.map(({category_id}) => category_id));
return category_id_array.every(id => cat_ids.has(id));
});
}

const res = filterPostsByCategoryIds([1, 4]);
console.log(res);

关于javascript - 普通 JavaScript : Filter nested array by array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61122278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com