Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda