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
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"));
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; }
I think this should help.
arr.filter(i => i.country === "Japan");
You can learn more about filter here.
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: { ...
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 ...
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, ...