过滤对象数组的最佳方法

时间:2019-02-09 16:36:54

标签: javascript arrays

我有一个对象数组,该数组具有要过滤的称为“国家/地区”的属性。我让用户选择他们想要查看的国家,并希望返回由该属性过滤的对象数组。完成此过程的最有效方法是什么?

  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 =>{

  ??  
 });

输出应该是包含匹配项的countrys数组的过滤版本

4 个答案:

答案 0 :(得分:2)

您可以使用includesfilter

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)

我宁愿将selectedCountries用作Object,而不是Array。不需要每次都在Array上循环。

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)

答案 1 :(得分:0)

您可以使用 Array.prototype.includes()

使用检查对象的国家/地区是否位于selectedCountries

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)

答案 2 :(得分:0)

使用Map通过Country(O(n))创建Array.reduce()。然后map请求的国家列表(O(m)),然后从地图中获得国家。复杂度为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);

答案 3 :(得分:0)

您可以使用array.indexOf函数

 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)