I have an array of country objects (countries)
and I’m trying to get just an array of the String
names, so from this example:
Just: ['Canada', 'USA', ..]
etc.
I’m trying to do this with
const getNames = (countries) => countries.map(({ Name }) => Name);
but I can’t seem to get it. Any suggestions?
Advertisement
Answer
You’re quite close to getting it right; just remove (countries) =>
and you’re good to go.
const getNames = countries.map(({ Name }) => Name);
Alternatively, you can keep your code, which is a function. To get the names of the countries – coNames
– call the function and pass countries
as a parameter.
const getNames = (countries) => countries.map(({ Name }) => Name); const countries = [{id: ...., Name:.........}]; const coNames = getNames( countries );
Your code is equivalent to:
const getNames = function( countries ) { return countries.map(function({Name}) { return Name; }); };