Javascript Arrays, are very useful for storing same data-type multiple values, and we may want to filter javascript array with multiple values, so in this article, I have mentioned how we can filter filter an array of object with multiple values in Javascript with examples.

To Filter Array with multiple values in Javascript, we will need to consider following steps:

  1. Use Array.filter() method on the array.
  2. Use operators like '&&' Or '||' to add multiple conditions, depending on our needs.
  3. Then We will get our required results from Array.

So, basically, code to filter values would look like below

// REQUIRED multiple conditions
const filteredData = myArray.filter(function (element) {
  return // condition1 && condition2 && condition3...
});

// OPTIONAL multiple conditions
const filteredData = myArray.filter(function (element) {
  return // condition1 || condition2 || condition3...
});

Here is an simple example, considering above points.

var students = [
  {name: 'Vikram', class: 9},
  {name: 'Vinod', class: 9},
  {name: 'Bhanu', class: 8},
];

var results = students.filter(element => {
  // using AND (&&) operator
  return element.class === 9 && element.name.includes("V");
});

//output: [{class: 9,name: "Vikram"}, {class: 9,name: "Vinod"}]
console.log(results);

Here is the fiddle: https://jsfiddle.net/0o3afg2r/

javascript-filter-multiple-array-values

If you want to add multiple conditions using "||", then we can have example code as below

var students = [
  {name: 'Vikram', class: 9},
  {name: 'Vinod', class: 9},
  {name: 'Bhanu', class: 8},
];

var results = students.filter(element => {
  // using OR(||) operator
  return element.class === 8 || element.name == 'Bhanu';
});

//output:[{class: 8,name: "Bhanu"}]
console.log(results);

As you can see in the above code, we are checking for OR (||) condition, means, any 1 of the 2 conditions can be true and it should return filtered values.

You may also like to read:

Top Best React Component libraries

Change Text color on click using Javascript

What is JQuery IsNumeric like function in Javascript?

Read XML using Javascript

Uncaught Error: "Cannot use import statement outside a module"

Get enum key by value in Typescript

Useful Javascript Unit Testing Frameworks

Best Javascript Charting Libraries