使用Union和Intersection

时间:2017-10-31 17:21:34

标签: javascript lodash

const myJSON = {
  "seller1": [
    {
      "product": "headphones",
      "price": 23,
      "weight": 1
    },
    {
      "product": "earphone",
      "price": 12,
      "weight": 1
    },
    {
      "product": "iPhone",
      "price": 999,
      "weight": 3
    },
    {
      "product": "ipad",
      "price": 399,
      "weight": 4
    }
  ],
  "seller2": [
    {
      "product": "headphones",
      "price": 25,
      "weight": 1
    },
    {
      "product": "earphone",
      "price": 10,
      "weight": 1
    },
    {
      "product": "iPhone",
      "price": 949,
      "weight": 2
    },
    {
      "product": "ipad",
      "price": 449,
      "weight": 3
    }
  ]
}

我需要从多选框中过滤myJSON,如果用户选择产品:iPhone和产品:耳机,我需要显示所有匹配iPhone和耳机的产品。在这种情况下,myJSON将是

const myJSON = {
  "seller1": [
    {
      "product": "headphones",
      "price": 23,
      "weight": 1
    },
    {
      "product": "iPhone",
      "price": 999,
      "weight": 3
    }
  ],
  "seller2": [
    {
      "product": "headphones",
      "price": 25,
      "weight": 1
    },
    {
      "product": "iPhone",
      "price": 949,
      "weight": 2
    },
  ]
}

现在,如果用户选择不同的密钥,价格:449,myJSON现在应该是空的,因为没有房产价格:449在那。但是,如果价格:23那么,

const myJSON = {
  "seller1": [
    {
      "product": "headphones",
      "price": 23,
      "weight": 1
    }
  ]
}

因此,例如,如果用户选择具有相同键(产品)的3个项目,我们应该显示与原始json中的这3个项目匹配的所有三个项目。现在,当他选择不同的密钥(价格)时,我们只应该从之前的结果中过滤。

非常感谢任何关于如何实现这一点的建议。

0 个答案:

没有答案