Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Get Names of Array of Objects without Looping

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:enter image description here

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just this?

const getNames = countries.map(c => c.Name); 
about 4 years ago · Juan Pablo Isaza Report

0

While topic says "without" looping, actually you can't. Map method also creates internal looping algorithm. So basically you can do something very simple and really efficient like standard for loop.

// Array of objects
const arr = [
  { id: 1, Name: "Canada", isSelected: false },
  { id: 2, Name: "USA", isSelected: false },
  { id: 3, Name: "India", isSelected: false }
];

// Function to get all params by name
const getAllParam = (arr, param, res = []) => {
  for(const e of arr) if(e[param]) res.push(e[param]);
  return res;
};

// Get all params with "Name" key
const res = getAllParam(arr, "Name");

// Test
console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

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;
    });
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!