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

148
Vistas
How do I set value of nested object to key of current object?

This array has the key to substitute with nested key of 'name'

const arr = ['status', 'user', ...]  <-- This array contains key to be replaced with name 

This is what my current response object is

[
  {
    "id": 11,
    "location": "Mumbai",
    "status": {
      "name": "NEW"
    },
    "user": {
      "name": "Rakesh"
     }
  }
]

How do I modify the above array of objects to this below

[
  {
    "id": 11,
    "location": "Mumbai",
    "status": "NEW",
    "user": "Rakesh"
  }
]

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

can try below code

const keys = ['status', 'user']
let arr = [
  {
    "id": 11,
    "location": "Mumbai",
    "status": {
      "name": "NEW"
    },
    "user": {
      "name": "Rakesh"
     }
  }
]

arr.map(a => keys.forEach(k => {
   if(a[k] && a[k].name) a[k] = a[k].name
}));

console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'd try this one:

const results = [
  {
    "id": 11,
    "location": "Mumbai",
    "status": {
      "name": "NEW"
    },
    "user": {
      "name": "Rakesh"
    }
  }, {
    "id": 12,
    "location": "Helsinki",
    "status": {
      "name": "NEW"
    },
    "user": {
      "name": "Samuli"
    }
  }
];

const flattenObject = ([key, value]) => ((typeof value === 'object') ? {[key] : value[Object.keys(value)[0]]} : {[key]: value});

const reduceToSingleObject = (acc, b) => ({...acc, ...b});

const actualResults = results.map((result) => Object.entries(result).map(flattenObject).reduce(reduceToSingleObject));


console.log(actualResults);

Explanation:

  1. flattenObject is a function to flatten structure of object inside object. This only takes the first prop (without knowing the name of the key). If you, for some reason, would need to flatten several key-values, then it'd need whole different kind of helper function to sort that out.

  2. reduceToSingleObject is a function to put all the key-value pairs into a single object. This could have been done already in flattenObject function, but for the clarity, I separated it to a normal map - reduce pattern.

  3. actualResults is the outcome where we go through all the entries of your original results.

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