kleineties
kleineties

Reputation: 47

Javascript filter array whitin array with multiple conditions

This is my outcome from the server.

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' } 
          ] 
         } 
      ]

And i need to filter it with multiple conditions.

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

I made a function to filter the data but this works only with one condition

  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

    }
}

What is the best way to loop through the array so both contions (filters) are met? The outcome needs to be only id 1 and 4 and thats needs to be in a return results array.

Upvotes: 0

Views: 77

Answers (3)

Jamiec
Jamiec

Reputation: 136259

Basically, you are looking for every item in arrays every item matches what your filters contains.

For this you can use every method on the filters and attempt to find the item with that name and match the value

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

Live example:

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);

Upvotes: 0

Mario Varchmin
Mario Varchmin

Reputation: 3782

This applies all the filters to each of the array elements. Only when all filter conditions are met, the array element is added to the result array.

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);

Upvotes: 1

Aman Gupta
Aman Gupta

Reputation: 172

Use this code if you want any of the filter condition will match

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

And use this code if you want all the filter conditions should match.

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

Upvotes: 0

Related Questions