Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda