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

223
Views
How to get the name of person by passing city property from array of JS object

I have this array of objects

const arrofobj = [
{city:"paris", name: "chris"},
{city:"berlin", name: "john"},
{city:"moscow", name: "jev"}
];

I want to write a function when I pass paris, I want to get back chris, how to achieve this?

Here is what I have tried

const arrofobj = [
{city:"paris", name: "chris"},
{city:"berlin", name: "john"},
{city:"moscow", name: "jev"}
];

function fetchname(cityname) {
  arrofobj.forEach((val)=>{if(Object.keys(val) == "city") {if (val.city == cityname) console.log(val.name)}})//nothing is printed
}

fetchname("paris");
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You able to use filter

const arrofobj = [
  {city:"paris", name: "chris"},
  {city:"berlin", name: "john"},
  {city:"moscow", name: "jev"}
];

console.log(arrofobj.filter((arro) =>arro.city == 'paris')[0].name);

about 4 years ago · Juan Pablo Isaza Report

0

you must use filter method as follow:

const arrofobj = [
    {city:"paris", name: "chris"},
    {city:"berlin", name: "john"},
    {city:"moscow", name: "jev"}
    ];
    
    function getByCity(city) {            
        var result = arrofobj.filter(obj => {
            return obj.city === city
        })
        return result;
    }

    console.log(getByCity('paris')[0].name)

about 4 years ago · Juan Pablo Isaza Report

0

I think it will help you Hvae you any question in this code. Comment below

    const arrofobj = [
  {city:"paris", name: "chris"},
  {city:"berlin", name: "john"},
  {city:"moscow", name: "jev"}
 ];

function fetchname(cityname) {
  const findValue = arrofobj.find((value)=>value.city==cityname)
    console.log(findValue)
    return findValue.name ?? ""
}

 fetchname("paris");
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!