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

162
Vistas
One liner javascript function to edit value of specific property from array of objects

Say, I have array of objects:

arr = [
    {path: 'mexico', name: 'mexico'},
    {path: 'brazil', name: 'brazil'},
    {path: 'netherlands', name: 'netherlands'}
];

What I want to achieve:

prefix = 'country/'
arr = [
    {path: 'country/mexico', name: 'mexico'},
    {path: 'country/brazil', name: 'brazil'},
    {path: 'country/netherlands', name: 'netherlands'}
];

Is there a one liner javascript function that append the prefix country/ into the path property of all objects in the array?

perhaps, something like this: arr.append(prefix, arr, 'path');

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

As mdn says about map() function:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

So the code would be looked like that:

arr = arr.map(s => ({...s, path: 'country/' + s.path}))

An example:

let arr = [
    {path: 'mexico', name: 'mexico'},
    {path: 'brazil', name: 'brazil'},
    {path: 'netherlands', name: 'netherlands'}
];

arr = arr.map(s => ({...s, path: 'country/' + s.path}))
console.log(arr)

about 4 years ago · Santiago Trujillo Denunciar

0

You could use a forEach and a concat:

arr = [
    {path: 'mexico', name: 'mexico'},
    {path: 'brazil', name: 'brazil'},
    {path: 'netherlands', name: 'netherlands'}
];
prefix = 'country/';
arr.forEach(x => x.path = prefix.concat(x.path));
console.log(arr);

EDIT:

Following what @MaikLowrey suggested on comment and reading this answer, + operator to concat strings has better performance and memory allocation compared to concat operator.

So the optimized solution is:

    arr = [
        {path: 'mexico', name: 'mexico'},
        {path: 'brazil', name: 'brazil'},
        {path: 'netherlands', name: 'netherlands'}
    ];
    prefix = 'country/';
    arr.forEach(x => x.path = prefix + x.path);
    console.log(arr);

about 4 years ago · Santiago Trujillo Denunciar

0

arr = [
    {path: 'mexico', name: 'mexico'},
    {path: 'brazil', name: 'brazil'},
    {path: 'netherlands', name: 'netherlands'}
];

prefix = "country/"

arr.forEach(entry => entry.path = prefix + entry.path)
console.log(arr)

about 4 years ago · Santiago Trujillo 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