具有多个条件的javascript筛选器数组whitin数组

0g0grzrc  于 2021-09-23  发布在  Java
关注(0)|答案(3)|浏览(366)

这是我从服务器得到的结果。

arrays= 
      [ 0: 
        { Id: 1
        , name: 'test1'
        , parameters: 
          [ { name: 'application_instance', value: 'home',              type: 'generic' } 
          , { name: 'application_name',     value: 'initial',           type: 'generic' } 
          , { name: 'application_role',     value: 'disk_init_role',    type: 'generic' } 
          , { name: 'customer_environment', value: 'development',       type: 'generic' } 
          , { name: 'customer_name',        value: 'guest',             type: 'generic' } 
        ] } 
      , 1: 
        { Id: 2
        , name: 'test2'
        , parameters: 
          [ { name: 'application_instance', value: 'home',              type: 'generic' } 
          , { name: 'application_name',     value: 'disk_init',         type: 'generic' } 
          , { name: 'application_role',     value: 'initial',           type: 'generic' } 
          , { name: 'customer_environment', value: 'development',       type: 'generic' } 
          , { name: 'customer_name',        value: 'guest',             type: 'generic' } 
        ] } 
      , 2:
        { Id: 3
        , name: 'test3'
        , parameters: 
          [ { name: 'application_instance', value: 'home',              type: 'generic' } 
          , { name: 'application_name',     value: 'initial',           type: 'generic' } 
          , { name: 'application_role',     value: 'initial',           type: 'generic' } 
          , { name: 'customer_environment', value: 'development',       type: 'generic' } 
          , { name: 'customer_name',        value: 'guest',             type: 'generic' } 
        ] } 
      , 3: 
        { Id: 4
        , name: 'test4'
        , parameters: 
          [ { name: 'application_instance', value: 'home',              type: 'generic' } 
          , { name: 'application_name',     value: 'initial',           type: 'generic' } 
          , { name: 'application_role',     value: 'disk_init_role',    type: 'generic' } 
          , { name: 'customer_environment', value: 'development',       type: 'generic' } 
          , { name: 'customer_name',        value: 'guest',             type: 'generic' } 
        ] } 
      , 5:
        { Id: 5
        , name: 'test5'
        , parameters: 
          [ { name: 'application_instance', value: 'home',              type: 'generic' } 
          , { name: 'application_name',     value: 'initial_disk_init', type: 'generic' } 
          , { name: 'application_role',     value: 'disk_initial',      type: 'generic' } 
          , { name: 'customer_environment', value: 'development',       type: 'generic' } 
          , { name: 'customer_name',        value: 'guest',             type: 'generic' } 
          ] 
         } 
      ]

我需要用多种条件来过滤它。

filters = {
  application_name: 'initial',
  application_role: 'disk_init_role'
}

我做了一个函数来过滤数据,但这只适用于一个条件

filterArray (array, filters) {
    for (var [key, value] of Object.entries(filters)) {
      const res = Object.values(array).filter(
        item => item.parameters.some(param => param.name === `${key}` && param.value === `${value}`)
      )

      return results

    }
}

在阵列中循环以满足两个contion(过滤器)的最佳方式是什么?结果只需要id 1和4,并且需要在返回结果数组中。

czfnxgou

czfnxgou1#

基本上,您正在查找中的每一项 arrays 每一项都符合你的要求 filters 包含。
为此,您可以使用 every 关于 filters 并试图 find 有那个的项目 name 并匹配 value ```
const result = arrays.filter(item => {
return Object.entries(filters)
.every( ([key,value]) => item.parameters.find(p => p.name == key).value == value)
});

示例:

const arrays=
[
{ Id: 1
, name: 'test1'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 2
, name: 'test2'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'disk_init', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 3
, name: 'test3'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 4
, name: 'test4'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
,
{ Id: 5
, name: 'test5'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial_disk_init', type: 'generic' }
, { name: 'application_role', value: 'disk_initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
]
}
]

const filters = {
application_name: 'initial',
application_role: 'disk_init_role'
}

const result = arrays.filter(item => {
return Object.entries(filters).every( ([key,value]) => item.parameters.find(p => p.name == key).value == value)
});

console.log(result);

yr9zkbsy

yr9zkbsy2#

这会将所有过滤器应用于每个数组元素。仅当满足所有筛选条件时,才会将数组元素添加到结果数组中。

const arrays= [
  {
    "0": {
      "Id": 1,
      "name": "test1",
      "parameters": [
        {
          "name": "application_instance",
          "value": "home",
          "type": "generic"
        },
        {
          "name": "application_name",
          "value": "initial",
          "type": "generic"
        },
        {
          "name": "application_role",
          "value": "disk_init_role",
          "type": "generic"
        },
        {
          "name": "customer_environment",
          "value": "development",
          "type": "generic"
        },
        {
          "name": "customer_name",
          "value": "guest",
          "type": "generic"
        }
      ]
    }
  },
  {
    "1": {
      "Id": 2,
      "name": "test2",
      "parameters": [
        {
          "name": "application_instance",
          "value": "home",
          "type": "generic"
        },
        {
          "name": "application_name",
          "value": "disk_init",
          "type": "generic"
        },
        {
          "name": "application_role",
          "value": "initial",
          "type": "generic"
        },
        {
          "name": "customer_environment",
          "value": "development",
          "type": "generic"
        },
        {
          "name": "customer_name",
          "value": "guest",
          "type": "generic"
        }
      ]
    }
  },
  {
    "2": {
      "Id": 3,
      "name": "test3",
      "parameters": [
        {
          "name": "application_instance",
          "value": "home",
          "type": "generic"
        },
        {
          "name": "application_name",
          "value": "initial",
          "type": "generic"
        },
        {
          "name": "application_role",
          "value": "initial",
          "type": "generic"
        },
        {
          "name": "customer_environment",
          "value": "development",
          "type": "generic"
        },
        {
          "name": "customer_name",
          "value": "guest",
          "type": "generic"
        }
      ]
    }
  },
  {
    "3": {
      "Id": 4,
      "name": "test4",
      "parameters": [
        {
          "name": "application_instance",
          "value": "home",
          "type": "generic"
        },
        {
          "name": "application_name",
          "value": "initial",
          "type": "generic"
        },
        {
          "name": "application_role",
          "value": "disk_init_role",
          "type": "generic"
        },
        {
          "name": "customer_environment",
          "value": "development",
          "type": "generic"
        },
        {
          "name": "customer_name",
          "value": "guest",
          "type": "generic"
        }
      ]
    }
  },
  {
    "5": {
      "Id": 5,
      "name": "test5",
      "parameters": [
        {
          "name": "application_instance",
          "value": "home",
          "type": "generic"
        },
        {
          "name": "application_name",
          "value": "initial_disk_init",
          "type": "generic"
        },
        {
          "name": "application_role",
          "value": "disk_initial",
          "type": "generic"
        },
        {
          "name": "customer_environment",
          "value": "development",
          "type": "generic"
        },
        {
          "name": "customer_name",
          "value": "guest",
          "type": "generic"
        }
      ]
    }
  }
];
const filters = {
  application_name: 'initial',
  application_role: 'disk_init_role'
};

const result = arrays.filter((e) => {
  const parameters = e[Object.keys(e)[0]].parameters;
  let match = true;
  Object.keys(filters).forEach((f) => {
    const parameter = parameters.find((g) => g.name === f);
    match = match && parameter.value === filters[f];
  });
  return match;
});

console.log(result);
ljo96ir5

ljo96ir53#

如果希望任何筛选条件匹配,请使用此代码

arr.filter(el => {
    const res = el.parameters.find(perameter => { 
        return filters[perameter.name] == perameter.value;
    });
    return !!res;
});

如果希望所有筛选条件都匹配,请使用此代码。

arr.filter(el => {
    const res = el.parameters.filter(perameter => { 
        return filters[perameter.name] == perameter.value;
    });
    return res.length === Object.keys(filters).length;
});

相关问题