0

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.

1
  • What do you mean by "this works only with one condition" ? I don't see any problem with your filter function if it only needs to check the "name" and "key" properties Commented Jul 9, 2021 at 13:03

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thx for your help this seems to work. :-)
0

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

2 Comments

why not use some and every - that's what they're for.
because If we implement this with some and every method then we have use an extra loop, like your code has 3 loops and my code have 2 loops, Which I think is not efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.