javascript - 通过单值js过滤对象

标签 javascript arrays json reactjs filter

我有下面的 JSON 数据:

{
    "items": [
        {
            "id": 3844,
            "sku": "aaa",
            "name": "aaa",
            "attribute_set_id": 4,
            "price": 2222,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "custom_attributes": [
                {
                    "attribute_code": "special_price",
                    "value": "2222.000000"
                },
                {
                    "attribute_code": "brand",
                    "value": "5465"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "75",
                        "791",
                        "793",
                        "799"
                    ]
                },
                {
                    "attribute_code": "color",
                    "value": "5447"
                },
                {
                    "attribute_code": "size",
                    "value": "5432"
                }
            ]
        },
        {
            "id": 3843,
            "sku": "HV 119",
            "name": "Engine valve-Bajaj Pulsar 180cc DTSi",
            "attribute_set_id": 4,
            "price": 292,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2020-04-06 07:48:29",
            "updated_at": "2020-04-06 07:48:29",
            "weight": 0.25,
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "special_price",
                    "value": "292.000000"
                },
                {
                    "attribute_code": "brand",
                    "value": "777"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "75",
                        "793",
                        "1350"
                    ]
                }
            ]
        },
        {
            "id": 3842,
            "sku": "HV 118",
            "name": "Engine valve-Bajaj Pulsar 150cc DTSi / DIS.125",
            "attribute_set_id": 4,
            "price": 274,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2020-04-06 07:46:01",
            "updated_at": "2020-04-06 07:46:01",
            "weight": 0.25,
            "tier_prices": [],
            "custom_attributes": [               
                {
                    "attribute_code": "special_price",
                    "value": "274.000000"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "75",
                        "793",
                        "1350"
                    ]
                }
            ]
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "75",
                        "condition_type": "eq"
                    }
                ]
            }
        ],
        "sort_orders": [
            {
                "field": "position",
                "direction": "DESC"
            }
        ],
        "page_size": 3,
        "current_page": 1
    },
    "total_count": 3
}

我的值是777。我需要上述数据,第一个元素有一个 custom_attributes>attribute_code === Brand 数组,它的值 === 777。我必须以这种方式进行过滤。同样在第二个数据中,我什至不存在 custom_attributes->attribute_code === Brand 。所以我应该删除这些数据,最后需要一个过滤后的数组。另外,在其他情况下,custom_attribute 中存在与数组 category_ids 匹配的数据,该数组是一个数组。因此,使用像 793 这样的特定值,我还需要进行过滤。

我尝试过的代码如下

 let overAll = filt.items.filter(c => c.custom_attributes.map((i,j)=>{
                if(i.attribute_code === "brand"){
                    return i.value === 777
                }
            }))

扩展输出:

{
    "items": [
        {
            "id": 3844,
            "sku": "aaa",
            "name": "aaa",
            "attribute_set_id": 4,
            "price": 2222,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "custom_attributes": [
                {
                    "attribute_code": "special_price",
                    "value": "2222.000000"
                },
                {
                    "attribute_code": "brand",
                    "value": "777"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "75",
                        "791",
                        "793",
                        "799"
                    ]
                },
                {
                    "attribute_code": "color",
                    "value": "5447"
                },
                {
                    "attribute_code": "size",
                    "value": "5432"
                }
            ]
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "75",
                        "condition_type": "eq"
                    }
                ]
            }
        ],
        "sort_orders": [
            {
                "field": "position",
                "direction": "DESC"
            }
        ],
        "page_size": 3,
        "current_page": 1
    },
    "total_count": 3
}

最佳答案

你可以试试这个过滤器吗?

function filter(brand,no){
        return el.items.filter(x=>x.custom_attributes.find(y=>y.attribute_code==brand && (y.value==no  || y.value.includes(no)) ))
}

并调用它

filter("brand",777)

关于javascript - 通过单值js过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61174571/

相关文章:

javascript - 如何在不删除子表内容的情况下删除父表?

javascript - 按名称对数组数据进行分组

java - 将 Json 数组映射到 Java 模型

php - jquery中如何读取json数组中的数据?

javascript - 如果 xml 包含重复(多个)标签,如何在 json 中表示 xml

Javascript 网络摄像头捕获并使用 PHP 上传到服务器

javascript - 没有插件的 Jekyll 照片库

javascript - 该网站无法使用 loadmore 按钮

javascript - 切片 JSON 数组以获取前五个对象

javascript - 将项目添加到数组开头而不使用 unshift 方法的函数