javascript - 如何根据多个条件过滤嵌套的对象数组?

标签 javascript node.js ecmascript-6

下面是对象的示例数组。 我希望根据 criteriaTypeidsource 对其进行过滤。如果没有一个 input.source 匹配,则应过滤掉父对象。此外,所有过滤条件都是可选的。

[{
    "id": "9be6c6299cca48f597fe71bc99c37b2f",
    "caption": "caption1",
    "criteriaType": "type2",
    "input": [
        {
            "id_1": "66be4486ffd3431eb60e6ea6326158fe",
            "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
            "source": "type1",
        },
        {
            "id_1": "1ecdf410b3314865be2b52ca9b4c8539",
            "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
            "source": "type2",
        }
    ]
},
{
    "id": "b83b3f081a7b45e087183740b12faf3a",
    "caption": "caption1",
    "criteriaType": "type1",
    "input": [
        {
            "id_1": "f46da7ffa859425e922bdbb701cfcf88",
            "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
            "source": "type3",
        },
        {
            "id_1": "abb87219db254d108a1e0f774f88dfb6",
            "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
            "source": "type1",
        }
    ]
},
{
    "id": "fe5b071a2d8a4a9da61bbd81b9271e31",
    "caption": "caption1",
    "criteriaType": "type1",
    "input": [
        {
            "id_1": "7ea1b85e4dbc44e8b37d1110b565a081",
            "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
            "source": "type3",
        },
        {
            "id_1": "c5f943b61f674265b8237bb560cbed03",
            "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
            "source": "type3",
        }
    ]
}]

我能够通过criteriaTypeid实现过滤。但我也无法按 source 进行过滤,以确保如果没有一个 input.source 匹配,则不会返回父级。

var json = <<array of objects>> ;
const {objectId: id, ctype: criteriaType, inputSource: source } = param; // getting the the params
json = ctype ? json.filter(({criteriaType}) => criteriaType === ctype ): json;
json = (objectId ? json.filter(({id}) => id === objectId ): json)
       .map (({id, caption, criteriaType, input }) => {
         //some manipulation 
         return { //results after manipulation}
       })

帮帮我吧!提前致谢。我不确定我们是否可以链接过滤器来实现它。

寻找 esLint 兼容代码

最佳答案

要求过滤器是可选的,并且不返回任何与父级匹配的源 https://jsfiddle.net/cpk18dt4/9/

注释在代码中。希望它能解释该函数的作用。

const fnFilter = (criteriaType, id, source) => {
  let result = oData;

  if (criteriaType) { // it can be null (optional)
    result = result.filter(d => d.criteriaType === criteriaType);
  }
  if (id) { // it can be null (optional)
    result = result.filter(d => d.id === id);
  }
  if (source) { // it can be null (optional)
    result = result.filter(d => {
      const inputs = d.input.filter(inp => inp.source === source);

      // If none of the input.source match, the parent object should be filtered out
      if (inputs.length === 0) {
        return false;
      }
      d.input = inputs;
      return true;
    });
  }

  return result;
};

关于javascript - 如何根据多个条件过滤嵌套的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51528469/

相关文章:

javascript - 对我的 Web 服务的 Ajax 查询在我的 json 中返回 xml

javascript - 如何正确记录通过 Object.defineProperties 添加的实例成员?

javascript - 如果警报已经存在,确保警报框不会弹出的最佳方法是什么?

javascript - 无法对循环中的最后一项应用函数

node.js - 从 Google App Engine - NodeJS Flexible Environment 访问 Google 表格和 Google 的非云平台 API

javascript - 如何在嵌套 Promise Cloud Functions 中确定 Firebase 的范围

node.js paypal https 请求

javascript - 本地存储在React应用程序中不是持久的

javascript - es6类中 'this'的值是什么意思

javascript - 如何在 javascript/nodejs (ES6) 中创建静态函数/对象