Reputation: 309
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
Upvotes: 2
Views: 93
Reputation: 36564
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"));
Upvotes: 2
Reputation: 16908
Yes you can only use Array.filter
for this.
Using the destructuring assignment {country}
in the arrow function, just pluck the country
property from the array of objects and use it for comparison with the supplied country filter criteria:
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
Upvotes: 0
Reputation: 386540
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; }
Upvotes: 1