-2

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

jon
  • 407
  • 4
  • 13

4 Answers4

2

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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You can use check if the Country of the object is in selectedCountries using Array.prototype.includes()

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"];
const filteredCountries = countries.filter(count => selectedCountries.includes(count.Country))
console.log(filteredCountries)
StudioTime
  • 22,603
  • 38
  • 120
  • 207
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

Create a Map by Country (O(n)) using Array.reduce(). Then map the list of requested countries (O(m)), and get the country from the Map. The complexity would be O(n + m).

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

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

const countryMap = countries.reduce((m, o) => m.set(o.Country, o), new Map);

const filteredCountries = selectedCountries.map(c => countryMap.get(c));

console.log(filteredCountries);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

You can use of array.indexOf function

 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 =>selectedCountries.indexOf(country.Country)!=-1);
 console.log(filteredCountries)
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46