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

时间:2019-03-08 16:45:09

标签: javascript arrays woocommerce-rest-api

我试图根据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"
            }
        },

    ]

我正在尝试获取类别名称为“ Category Name”或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个不同的类别,需要在不同的时间致电。

1 个答案:

答案 0 :(得分: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)