我有一组对象需要通过另一个对象的属性进行过滤。
[
{
"_type": "attribute",
"name": "ARO",
"value": "28",
"description": "descrição apenas para teste",
"visualPresentation": null,
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8",
"label": "28"
},
{
"value": "29",
"description": null,
"visualPresentation": null,
"_type": "attribute",
"name": "ARO",
"sku": "trekMadoneSL7",
"label": "29"
},
{
"_type": "attribute",
"name": "ARO",
"value": "26",
"description": null,
"visualPresentation": null,
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8-test-copy",
"label": "26"
},
{
"description": null,
"visualPresentation": null,
"_type": "attribute",
"name": "ARO",
"value": "26",
"sku": "trekMadoneSL777",
"label": "26"
},
{
"visualPresentation": null,
"_type": "attribute",
"name": "ARO",
"value": "29",
"description": null,
"sku": "trekMadoneSL7777",
"label": "29"
},
{
"description": null,
"visualPresentation": {
"visualization": "InputToggleColor"
},
"_type": "attribute",
"name": "COR",
"value": "#9C7344",
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8",
"label": "#9C7344",
"className": "bg-[#9C7344]"
},
{
"_type": "attribute",
"name": "COR",
"value": "#FCFFFE",
"description": null,
"visualPresentation": null,
"sku": "trekMadoneSL7",
"label": "#FCFFFE",
"className": "bg-[#FCFFFE]"
},
{
"visualPresentation": null,
"_type": "attribute",
"name": "COR",
"value": "#FCAD03",
"description": null,
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8-test-copy",
"label": "#FCAD03",
"className": "bg-[#FCAD03]"
},
{
"_type": "attribute",
"name": "COR",
"value": "#FCFFFE",
"description": null,
"visualPresentation": null,
"sku": "trekMadoneSL777",
"label": "#FCFFFE",
"className": "bg-[#FCFFFE]"
},
{
"visualPresentation": null,
"_type": "attribute",
"name": "COR",
"value": "#FCFFFE",
"description": null,
"sku": "trekMadoneSL7777",
"label": "#FCFFFE",
"className": "bg-[#FCFFFE]"
},
{
"_type": "attribute",
"name": "TAMANHO",
"value": "M",
"description": null,
"visualPresentation": {
"visualization": "InputToggleButton"
},
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8",
"label": "M"
},
{
"value": "XS",
"description": null,
"visualPresentation": {
"visualization": "Dropdown"
},
"_type": "attribute",
"name": "TAMANHO",
"sku": "trekMadoneSL7",
"label": "XS"
},
{
"_type": "attribute",
"name": "TAMANHO",
"value": "S",
"description": null,
"visualPresentation": {
"visualization": "InputToggleButton"
},
"sku": "SKU-BICI-EST-TREKMAD-SL7-GEN8-test-copy",
"label": "S"
},
{
"_type": "attribute",
"name": "TAMANHO",
"value": "L",
"description": null,
"visualPresentation": null,
"sku": "trekMadoneSL777",
"label": "L"
},
{
"_type": "attribute",
"name": "TAMANHO",
"value": "L",
"description": null,
"visualPresentation": null,
"sku": "trekMadoneSL7777",
"label": "L"
}
]
ex:过滤数组并返回具有
中任何属性的任何对象{
"COR": "#FCFFFE",
"TAMANHO": "L",
"ARO": "29"
}
非常感谢您的帮助。预先感谢
我尝试过过滤器、chatgpt 等...但没有改变
您可以获取搜索对象的条目并过滤对象数组。
const
data = [...],
filterObject = { COR: "#FCFFFE", TAMANHO: "L", ARO: "29" },
search = Object.entries(filterObject),
result = data.filter(object => search.every(([key, value]) => object[key] === value));