方法过滤器JS

时间:2019-04-13 13:17:18

标签: javascript arrays

有一个函数会根据第二个参数返回一个数组(如果国家是日本,那么它将仅返回来自日本的汽车品牌)。是否可以仅使用过滤器方法来改善功能?

const arr = [
   {  
     "name":"BMW",
     "price":"55 000",
     "country":"Germany",
     "sertificate":"yes"
  },
  {  
    "name":"Mercedes-benz",
    "price":"63 000", 
    "country":"Germany",
    "certificate":"yes"
  },
  {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
  },
  {  
    "name":"TOYOTA", 
    "price":"48 000", 
    "max_people":"7",
    "country":"Japan",
    "certificate":"yes"
  },
  {  
    "name":"Volkswagen",
    "price":"36 000", 
    "constructor":"Pier Sun",
    "country":"Germany",
    "certificate":"no"
  },
 ];

 function getCountry(arr, country) {
   let obj = arr.filter(function(arr){
      return arr.country === country ? arr.country : '';
   });
   let itemCountry = [{}];
   let newItem = 0;
   Object.keys(obj).forEach (item => (obj[item]!==null) ? (itemCountry[newItem]=obj[item] , newItem++): '');
   return itemCountry;
}

console.log(getCountry(arr,"Japan"));  // or any other country

4 个答案:

答案 0 :(得分:3)

我认为filter()之后不需要任何代码。这里是使用Arrow Functions的版本

const arr = [ { "name":"BMW", "price":"55 000", "country":"Germany", "sertificate":"yes" }, { "name":"Mercedes-benz", "price":"63 000", "country":"Germany", "sertificate":"yes" }, { "name":"Mitsubishi", "price":"93 000", "constructor":"Bar John", "door":"3", "country":"Japan", }, { "name":"TOYOTA", "price":"48 000", "max_people":"7", "country":"Japan", "sertificate":"yes" }, { "name":"Volkswagen", "price":"36 000", "constructor":"Pier Sun", "country":"Germany", "sertificate":"no" }, ];

const getCountry = (arr, country) => arr.filter(x => x.country === country);
console.log(getCountry(arr,"Japan"));

答案 1 :(得分:2)

您可以继续前进,并使用抽象的解决方案,在该解决方案中,您可以添加键和所需的值来过滤数组。

const
    getItems = (array, key, value) => array.filter(o => o[key] === value),
    data = [{ name: "BMW", price: "55 000", country: "Germany", sertificate: "yes" }, { name: "Mercedes-benz", price: "63 000", country: "Germany", certificate: "yes" }, { name: "Mitsubishi", price: "93 000", constructor: "Bar John", door: "3", country: "Japan" }, { name: "TOYOTA", price: "48 000", max_people: "7", country: "Japan", certificate: "yes" }, { name: "Volkswagen", price: "36 000", constructor: "Pier Sun", country: "Germany", certificate: "no" }];

console.log(getItems(data, "country", "Japan"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

我认为这应该有所帮助。

arr.filter(i => i.country === "Japan");

您可以了解有关过滤器here的更多信息。

答案 3 :(得分:0)

是的,您只能为此使用Array.filter

在箭头函数中使用解构分配{country},只需从对象数组中抽取country属性,然后将其与提供的国家/地区过滤条件进行比较:

const arr = [ { "name":"BMW", "price":"55 000", "country":"Germany", "sertificate":"yes" }, { "name":"Mercedes-benz", "price":"63 000", "country":"Germany", "sertificate":"yes" }, { "name":"Mitsubishi", "price":"93 000", "constructor":"Bar John", "door":"3", "country":"Japan", }, { "name":"TOYOTA", "price":"48 000", "max_people":"7", "country":"Japan", "sertificate":"yes" }, { "name":"Volkswagen", "price":"36 000", "constructor":"Pier Sun", "country":"Germany", "sertificate":"no" }, ];
function getCountry(arr, filterCountry) {
   return arr.filter(({country}) => {
      return country === filterCountry;
   });
}

console.log(getCountry(arr,"Japan"));  // or any other country