Q:

Best way to filter the array of objects

I have an array of objects with a property I want to filter on called "Country". I have the user select which countries they want to see and wish to return an array of objects filtered by that property. What is the most efficient way to complete this process?
  var countries = [{GINI: 56.2, Country: "Central African Republic", Values: Array, Date: "2008"},
  {GINI: 51.3, Country: "Brazil", Values: Array, Date: "2015"},{GINI: 51.1, Country: "Columbia", Values: Array, Date: "2015"}]

 var selectedCountries = ["Central African Republic", "Columbia"];

 var filteredCountries = countries.filter(country =>{

  ??  
 });

Output should be a filtered version of the countries array containing the matches 

A:

You can use includes and filter

const countries = [{GINI: 56.2, Country: "Central African Republic", Values: Array, Date: "2008"},{GINI: 51.3, Country: "Brazil", Values: Array, Date: "2015"},{GINI: 51.1, Country: "Columbia", Values: Array, Date: "2015"}]
const selectedCountries = ["Central African Republic", "Columbia"];

const filteredCountries = countries.filter(({Country})=>selectedCountries.includes(Country));

console.log(filteredCountries)

I will prefer to have selectedCountries as an Object instead of an Array. Than need not to loop on Array every time.

const countries = [{GINI: 56.2, Country: "Central African Republic", Values: Array, Date: "2008"},{GINI: 51.3, Country: "Brazil", Values: Array, Date: "2015"},{GINI: 51.1, Country: "Columbia", Values: Array, Date: "2015"}]

const selectedCountries = {
 "Central African Republic": true,
 "Columbia": true
};

const filteredCountries = countries.filter(({Country})=>selectedCountries[Country]);

console.log(filteredCountries)

