如果对象包含另一个数组中存在值的属性,则过滤对象数组

时间:2020-03-11 09:47:11

标签: javascript arrays javascript-objects

我有以下对象数组。

const abc = [
      {
        sku: 1,
        features: ["Slim"],
        fields: [
          { label: "Material", value: "Material1" },
          { label: "Type", value: "Type1" },
        ]
      },
      {
        sku: 2,
        features: ["Cotton"],
        fields: [
          { label: "Material", value: "Material2" },
          { label: "Type", value: "Type1" },
        ]
      },
      {
        sku: 3,
        features: ["Cotton"],
        fields: [
          { label: "Material", value: "Material3" },
          { label: "Type", value: "Type2" },
        ]
      }
    ];

我只想过滤那些具有其特征和字段值的对象

const fieldsArr = ["Material1", "Material2", "Type1", "Slim"]

预期输出为

let output = [
      {
        sku: 1,
        features: ["Slim"],
        fields: [
          { label: "Material", value: "Material1" },
          { label: "Type", value: "Type1" },
        ]
      },
      {
        sku: 2,
        features: ["Cotton"],
        fields: [
          { label: "Material", value: "Material2" },
          { label: "Type", value: "Type1" },
        ]
      },
    ]

我解决了这样的功能部分

abc.forEach(e => {
      if (e.features.some(v => fieldsArr.indexOf(v) !== -1)) {
        output.push(e);
      }
    });

但是我在过滤字段部分时遇到问题。 有没有一种方法可以根据上述条件以优化的方式过滤对象。

2 个答案:

答案 0 :(得分:6)

您还需要迭代嵌套数组。

const
    abc = [{ sku: 1, features: ["Slim"], fields: [{ label: "Material", value: "Material1" }, { label: "Type", value: "Type1" }] }, { sku: 2, features: ["Cotton"], fields: [{ label: "Material", value: "Material2" }, { label: "Type", value: "Type1" }] }, { sku: 3, features: ["Cotton"], fields: [{ label: "Material", value: "Material3" }, { label: "Type", value: "Type2" }] }],        fieldsArr = ["Material1", "Material2", "Type1", "Slim"],
    result = abc.filter(({ features, fields }) =>
        features.some(v => fieldsArr.includes(v)) ||
        fields.some(({ value }) => fieldsArr.includes(value))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

使用过滤器,并使用fieldsArr检查featuresfields(值)的每个项目组合值。

const abc = [
  {
    sku: 1,
    features: ["Slim"],
    fields: [
      { label: "Material", value: "Material1" },
      { label: "Type", value: "Type1" }
    ]
  },
  {
    sku: 2,
    features: ["Cotton"],
    fields: [
      { label: "Material", value: "Material2" },
      { label: "Type", value: "Type1" }
    ]
  },
  {
    sku: 3,
    features: ["Cotton"],
    fields: [
      { label: "Material", value: "Material3" },
      { label: "Type", value: "Type2" }
    ]
  }
];

const fieldsArr = ["Material1", "Material2", "Type1", "Slim"];

const res = abc.filter(item =>
  [...item.features, ...item.fields.map(x => x.value)].some(fea =>
    fieldsArr.includes(fea)
  )
);

console.log(res);

// Update: more concise using destructure
const res2 = abc.filter(({features, fields}) =>
  [...features, ...fields.map(({value}) => value)].some(fea =>
    fieldsArr.includes(fea)
  )
);
console.log(res2);