JavaScript The method filter JS method filter javascript,method filter js,method filter does not

There is a function that returns an array according to the second argument (if the country is Japan, it will return only brands of cars from Japan). Is it possible to improve the function using only the filter method?

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
Answer:1

I think there is no need of any code after filter().Here is the version using 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"));
Answer:2

You could go a step ahead and use an abstract solution, where you can add a key and a wanted value for filtering the array.

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; }
Answer:3

I think this should help.

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

You can learn more about filter here.

Answer:4

I am working on ListControl and one of the columns has Delete link which I am formatting using HTMLTemplate as follows: <a href="javascript: app.showConfirmation()" >Delete</a> My ...

I am working on ListControl and one of the columns has Delete link which I am formatting using HTMLTemplate as follows: <a href="javascript: app.showConfirmation()" >Delete</a> My ...

I have added "domino" in the server.ts and even updated webpack.server.config.js as : module: { rules: [ { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { ...

I have added "domino" in the server.ts and even updated webpack.server.config.js as : module: { rules: [ { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { ...

  1. angular universal referenceerror document is not defined
  2. angular universal referenceerror window is not defined
  3. angular universal referenceerror localstorage is not defined
  4. angular universal referenceerror event is not defined
  5. angular universal referenceerror navigator is not defined
  6. angular universal referenceerror mouseevent is not defined
  7. angular ssr referenceerror document is not defined
  8. referenceerror document is not defined angular universal
  9. error referenceerror document is not defined angular universal

This question is regarding JavaScript. If the input is any of the following array "hello, hi, yo, or hey", I want the value to return true, but it is only returning false. I've tried multiple methods ...

This question is regarding JavaScript. If the input is any of the following array "hello, hi, yo, or hey", I want the value to return true, but it is only returning false. I've tried multiple methods ...

  1. iterate over array javascript
  2. iterate over array python
  3. iterate over array java
  4. iterate over array bash
  5. iterate over array golang
  6. iterate over array of objects javascript
  7. iterate over array ruby
  8. iterate over array c++
  9. iterate over array java 8
  10. iterate over array php
  11. iterate over array matlab
  12. iterate over array swift
  13. iterate over array typescript
  14. iterate over array react
  15. iterate over array perl
  16. iterate over array scala
  17. iterate over array jquery
  18. iterate over array rust
  19. iterate over array es6
  20. iterate over array powershell

I want set property's value for Polymer object from function, which depended from other properties, if default value not set Polymer({ is: "my-element", properties : { a: String, ...

I want set property's value for Polymer object from function, which depended from other properties, if default value not set Polymer({ is: "my-element", properties : { a: String, ...

  1. default value property access
  2. default value property c#
  3. default value property in aem
  4. default value property spring boot
  5. default value property
  6. default value property objective c
  7. default value property vb.net
  8. default value property javascript
  9. default value property spring
  10. default value property delphi
  11. default value property java
  12. default value property maven
  13. default value property pega
  14. default value property of field
  15. default property value typescript
  16. default property value react
  17. default property value attribute c#
  18. default property value vue
  19. default property value vb
  20. set default value property c#