1

const usersData = [
{
  
  "count": 10,
  "customerList": [
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Family Practice",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "General Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Internal Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Internal Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Family Practice",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    }
    
  ]
}
]

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']
let filteredProviderData = usersData[0].customerList.filter(function(e) {
  return filterKeyName.every(function(a) {
    return filterValue.includes(e[a])
  })
})
console.log(filteredProviderData)

Here there is a sample data for the users. Here my requirement is to filter the key with multiple values. here once you run the code using filterKeyName and filterValue as shown below

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']

you will get the output as displayed here.

[
  {
    "primarySpecialty": "Family Practice",
    "primarySpecialtyCode": "008",
    "gender": "M",
    "graduationYear": 2001,
    "regularOfficeHours": true,
    "extendedOfficeHours": true
  }
]

Here my requirement is to filter single key with multiple values means if primarySpecialty = ['Family Practice','General Medicine'] and Gender = ['F','M'] how to filter the usersData as shown into the code snippet.

Thanks in Advance.

1
  • This looks straight forward - just use those values you show (it's just 'is thing in array' for which .indexOf exists) in a function that you use with usersData.customerList.filter(...) to decide whether an object matches or not? That should be more than enough of a hint for you to write the rest of the code yourself. Commented Mar 30, 2019 at 17:35

1 Answer 1

2

You should use the current key and its index in order to make the desired comparison:

return e[k] === filterValue[i];

const usersData = [{    "count": 10,  "customerList": [    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Family Practice",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "General Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Internal Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Internal Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Family Practice",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    }      ]}]

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']
let filteredProviderData = usersData[0].customerList.filter(function(e) {
  return filterKeyName.every(function(k, i) {
    return e[k] === filterValue[i];
  })
})
console.log(filteredProviderData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty'] let filterValue = ['F',true,'Family Practice','Internal Medicine'] If I am using filterValue like this its not filtering It should be filtered using key primarySpeciality and value = ['Family Practice','Internal Medicine'] as per the filterValue

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.