根据子阵列中的对象值过滤对象数组

问题描述 投票:0回答:1

我试图根据WooCommerce Rest API中的对象子数组中的值来过滤对象数组,以进入我的React应用程序

这是数组的简化版本

    const arrayExample =[
        {
            "id": 1,
            "name": "Product Name 1",
            "status": "publish",
            "categories":[
                {
                    "id": 34,
                    "name": "Category Name",
                    "slug": "category-name"
                }
            ],
            "acf":{
                "data_throughput": "100"
            }
        },
        {
            "id": 2,
            "name": "Product Name 2",
            "status": "publish",
            "categories":[
                {
                    "id": 32,
                    "name": "Another Category Name",
                    "slug": "another-category-name"
                },
                {
                    "id": 35,
                    "name": "Other Category Name",
                    "slug": "other-category-name"
                },
            ],
            "acf":{
                "data_throughput": "10"
            }
        },
        {
            "id": 3,
            "name": "Product Name 3",
            "status": "publish",
            "categories":[
                {
                    "id": 31,
                    "name": "New Category Name",
                    "slug": "new-category-name"
                },
                {
                    "id": 32,
                    "name": "Another Category Name",
                    "slug": "another-category-name"
                },
                {
                    "id": 34,
                    "name": "Category Name",
                    "slug": "category-name"
                },
            ],
            "acf":{
                "data_throughput": "50"
            }
        },

    ]

我试图抓住每个类别名称为“类别名称”或ID为34的数组项。我尝试过使用过滤函数

const filterList = arrayExample.filter(info=>info.categories.name==="Category Name")

甚至

const filterList = arrayExample.filter(info=>info.categories.id===34)

但他们都回来了。我不能做info.categories [0],因为它并不总是在第一点。

我从我的网站上为这些项目调用了WooCommerce API,除了类别名称或ID之外,我找不到任何其他匹配标识符。我只是接近这整件事吗?我有大约5个不同的类别,我需要在不同的时间打电话。

javascript arrays woocommerce-rest-api
1个回答
3
投票

您将类别作为数组,因此您需要通过索引访问它。但是你试图直接访问而不指定任何索引。所以你可以简单地在过滤器中使用some

const arrayExample =[{"id": 1,"name": "Product Name 1","status": "publish","categories":[{"id": 34,"name": "Category Name","slug": "category-name"}],"acf":{"data_throughput": "100"}},{"id": 2,"name": "Product Name 2","status": "publish","categories":[{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 35,"name": "Other Category Name","slug": "other-category-name"},],"acf":{"data_throughput": "10"}},{"id": 3,"name": "Product Name 3","status": "publish","categories":[{"id": 31,"name": "New Category Name","slug": "new-category-name"},{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 34,"name": "Category Name","slug": "category-name"},],"acf":{"data_throughput": "50"}},]
    
let op = arrayExample.filter(({categories})=> categories.some(({id})=> id === 34))

console.log(op)
© www.soinside.com 2019 - 2024. All rights reserved.