【发布时间】:2019-10-20 06:18:44
【问题描述】:
我试图通过一个看起来像这样的对象
{
"nodes": [
{
"attributes": null
},
{
"attributes": {
"nodes": [
{
"attributeId": 1,
"name": "pa_color",
"options": [
"gray"
]
},
{
"attributeId": 2,
"name": "pa_size",
"options": [
"large"
]
}
]
}
},
{
"attributes": {
"nodes": [
{
"attributeId": 1,
"name": "pa_color",
"options": [
"blue"
]
}
]
}
}
]
}
到一个反应组件中,该组件以所有唯一名称呈现所有不同的选项。但是,数据的结构方式意味着我会收到重复的名称和选项。
我正在尝试将对象转换为这个对象
{
"node": {
"attributeId": 1,
"name": "pa_color",
"values": [
{
"name": "gray"
},
{
"name": "blue"
}
]
},
"node": {
"attributeId": 2,
"name": "pa_size",
"values": [
{
"name": "large"
}
]
},
}
当前代码如下所示
export interface Category_products_edges_node_attributes_edges_node {
__typename: "ProductAttribute";
/**
* Attribute Global ID
*/
name: string;
/**
* Attribute options
*/
options: (string | null)[] | null;
/**
* Attribute ID
*/
attributeId: number;
}
export interface ProductFiltersProps {
attributes: Category_products_edges_node_attributes_edges_node[]
}
export const ProductFilters: React.FC<ProductFiltersProps> = ({
attributes,
}) => (
<div className="product-filters">
<div className="container">
<div className="product-filters__grid">
{attributes.map(attribute => (
我已经尝试过了
{groupBy(attributes, 'attributeId').map(attribute => (
使用 Lodash 库,但收到错误
此表达式不可调用。类型 'Category_products_edges_node_attributes_edges_node[]' 没有调用 签名。
最好的方法是什么?
谢谢
【问题讨论】:
标签: javascript reactjs