简体   繁体   English

过滤对象数组的最佳方法

[英]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输出应该是包含匹配项的国家数组的过滤版本

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 .我更愿意将selectedCountries作为Object而不是Array Than need not to loop on Array every time.不需要每次都在 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)

You can use check if the Country of the object is in selectedCountries using Array.prototype.includes()您可以使用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)

Create a Map by Country (O(n)) using Array.reduce() .使用Array.reduce()Country (O(n)) 创建地图 Then map the list of requested countries (O(m)), and get the country from the Map.然后映射请求的国家列表 (O(m)),并从 Map 中获取国家。 The complexity would be O(n + 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);

You can use of array.indexOf function您可以使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM