首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤javascript中的嵌套JSON数组并返回符合条件的内部数组

过滤javascript中的嵌套JSON数组并返回符合条件的内部数组
EN

Stack Overflow用户
提问于 2019-06-27 04:49:23
回答 4查看 1.8K关注 0票数 0

我有这样一个json对象:

代码语言:javascript
复制
{
  "products": [
    {
      "ID": "001",
      "Attributes": [
        {
          "value": "BESTSELLERS",
          "identifier": "BEST_SELLER"
        },
        {
          "value": "Color",
          "identifier": "Green"
        },
        {
          "value": "Size",
          "identifier": "L"
        }
      ],
      "SKUs": [
        {
          "ID": "001_1",
          "Attributes": [
            {
              "value": "BESTSELLERS",
              "identifier": "BEST_SELLER"
            },
            {
              "value": "Color",
              "identifier": "Green"
            },
            {
              "value": "Size",
              "identifier": "L"
            }
          ]
        },
        {
          "ID": "001_2",
          "Attributes": [
            {
              "value": "BESTSELLERS",
              "identifier": "BEST_SELLER"
            },
            {
              "value": "Color",
              "identifier": "Yellow"
            },
            {
              "value": "Size",
              "identifier": "M"
            }
          ]
        }
      ]
    },
    {
      "ID": "002",
      "Attributes": [
        {
          "value": "BESTSELLERS",
          "identifier": "BEST_SELLER"
        },
        {
          "value": "Size",
          "identifier": "L"
        }
      ],
      "SKUs": [
        {
          "ID": "002_1",
          "Attributes": [
            {
              "value": "BESTSELLERS",
              "identifier": "BEST_SELLER"
            },
            {
              "value": "Color",
              "identifier": "Black"
            },
            {
              "value": "Size",
              "identifier": "L"
            }
          ]
        },
        {
          "ID": "002_2",
          "Attributes": [
            {
              "value": "BESTSELLERS",
              "identifier": "BEST_SELLER"
            },
            {
              "value": "Color",
              "identifier": "Grey"
            }
          ]
        }
      ]
    },
    {
      "ID": "003",
      "Attributes": [
        {
          "value": "BESTSELLERS",
          "identifier": "BEST_SELLER"
        },
        {
          "value": "Color",
          "identifier": "Blue"
        }
      ],
      "SKUs": []
    }
  ]
}')

如您所见,products是一个数组,它包含另一个数组SKUs,它包含另一个数组Attributes

我想得到所有具有属性的SKUs - SizeColor

所以,它应该会回来

SKUs 001_1, 001_2 and 002_1

因此,我编写了以下代码:

代码语言:javascript
复制
var obj = JSON.parse('<MyAboveJSONObjectHere>');
obj.products.filter( p => p.SKUs.filter(sku => sku.Attributes.filter(att => 
att.identifier === 'Color' && att.identifier === 'Size')))

但这将返回JSON中的所有JSON产品对象。

您能告诉我的代码表达式有什么问题吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-06-27 05:26:51

“使用reduce":

代码语言:javascript
复制
data.products.reduce((p, c) => (
  (c.SKUs = c.SKUs.filter(
    sku =>
      sku.Attributes.some(att => att.value === "Color") &&
      sku.Attributes.some(att => att.value === "Size")
  )).length && p.push(c), p ),[]
);

代码语言:javascript
复制
data = {
  products: [
    {
      ID: "001",
      Attributes: [
        {
          value: "BESTSELLERS",
          identifier: "BEST_SELLER"
        },
        {
          value: "Color",
          identifier: "Green"
        },
        {
          value: "Size",
          identifier: "L"
        }
      ],
      SKUs: [
        {
          ID: "001_1",
          Attributes: [
            {
              value: "BESTSELLERS",
              identifier: "BEST_SELLER"
            },
            {
              value: "Color",
              identifier: "Green"
            },
            {
              value: "Size",
              identifier: "L"
            }
          ]
        },
        {
          ID: "001_2",
          Attributes: [
            {
              value: "BESTSELLERS",
              identifier: "BEST_SELLER"
            },
            {
              value: "Color",
              identifier: "Yellow"
            },
            {
              value: "Size",
              identifier: "M"
            }
          ]
        }
      ]
    },
    {
      ID: "002",
      Attributes: [
        {
          value: "BESTSELLERS",
          identifier: "BEST_SELLER"
        },
        {
          value: "Size",
          identifier: "L"
        }
      ],
      SKUs: [
        {
          ID: "002_1",
          Attributes: [
            {
              value: "BESTSELLERS",
              identifier: "BEST_SELLER"
            },
            {
              value: "Color",
              identifier: "Black"
            },
            {
              value: "Size",
              identifier: "L"
            }
          ]
        },
        {
          ID: "002_2",
          Attributes: [
            {
              value: "BESTSELLERS",
              identifier: "BEST_SELLER"
            },
            {
              value: "Color",
              identifier: "Grey"
            }
          ]
        }
      ]
    },
    {
      ID: "003",
      Attributes: [
        {
          value: "BESTSELLERS",
          identifier: "BEST_SELLER"
        },
        {
          value: "Color",
          identifier: "Blue"
        }
      ],
      SKUs: []
    }
  ]
};

console.log(data.products.reduce((p, c) => (
    (c.SKUs = c.SKUs.filter(
      sku =>
        sku.Attributes.some(att => att.value === "Color") &&
        sku.Attributes.some(att => att.value === "Size")
    )).length && p.push(c), p ),[]
));

票数 1
EN

Stack Overflow用户

发布于 2019-06-27 05:18:57

如果我正确理解,您希望从所提供的ID数组中获得SKU products值的列表,其中这些SKU项具有包含"Color""Size"值的Attribute子数组。

这可以通过reduce()来实现,其中约简回调根据属性标准过滤SKU项。然后将任何筛选的SKU项映射到其ID字段,并(通过concat())收集到生成的output数组,如下所示:

代码语言:javascript
复制
const obj={"products":[{"ID":"001","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Green"},{"value":"Size","identifier":"L"}],"SKUs":[{"ID":"001_1","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Green"},{"value":"Size","identifier":"L"}]},{"ID":"001_2","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Yellow"},{"value":"Size","identifier":"M"}]}]},{"ID":"002","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Size","identifier":"L"}],"SKUs":[{"ID":"002_1","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Black"},{"value":"Size","identifier":"L"}]},{"ID":"002_2","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Grey"}]}]},{"ID":"003","Attributes":[{"value":"BESTSELLERS","identifier":"BEST_SELLER"},{"value":"Color","identifier":"Blue"}],"SKUs":[]}]};

/* Filter each product by the required SKU/attribute criteria */
const result = obj.products.reduce((output, product) => {

  /* Determine if this products SKUs have contain requied attribute values */
  return output.concat(product.SKUs.filter((sku) => {

    const attributes = sku.Attributes;

    /* Search the attributes of this sku, looking for any with values that
    are color or size */
    const hasColor = attributes.some((attribute) => attribute.value === 'Color');
    const hasSize = attributes.some((attribute) => attribute.value === 'Size');

    /* If both attribute values found then this sku matches required criteria */
    return hasColor && hasSize;
  })
  /* Map any filtered sku to it's ID and concat the result to output */
  .map(sku => sku.ID));

}, []);

console.log(result);

票数 1
EN

Stack Overflow用户

发布于 2019-06-27 05:01:58

代码语言:javascript
复制
obj.products.map( p => {
       return p.SKUs.map(sku => {
    return sku.Attributes.filter(att => att.identifier === 'Color' && att.identifier === 'Size')}}

尝试这样做,基本上您需要首先通过数组进行map,并且只需要在最后一个filter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56784074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档