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

86
Vistas
moving a key value pair out of an array

I am trying to move everything in the Array Results outside and into the original object

this is the object

{
  "Name": "John",
  "Results": [
    {
      "Type": "DB",
      "Immediate_Action": "No",
    }
  ]
}

It should look like this

{
  "Name": "John",
  "Type": "DB",
  "Immediate_Action": "No",
}

What I have so far is this

const mapOscarResults = ({ data }) => {
    return data.map(entry => {
        let mapped = {...entry};
        entry.Results.forEach(key => {
            let Type =  mapped[key.Type]

            if (mapped[key]) {
                mapped[key].push(entry.Results[key]);
            } else {
                mapped[key] = [entry.Results[key]];
            }
        });
        return mapped;
    });
};
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You can simply spread the Results array into an Object.assign() call.

const input = { "Name": "John", "Results": [{ "Type": "DB", "Immediate_Action": "No", }, { "Another": "value" }] };

const { Results, ...refactored } = input;
Object.assign(refactored, ...Results);

console.log(refactored)

about 4 years ago · Santiago Gelvez Denunciar

0

however, it will join first Result with index 0, you can expand it

const data = {
    "Name": "John",
    "Results": [
        {
            "Type": "DB",
            "Immediate_Action": "No",
        }
    ]
}

const mapOscarResults = (data) => {
for (let i in Object.keys(data)){
    if (Array.isArray(data[Object.keys(data)[i]])){
        newKey = data[Object.keys(data)[i]][0]
        data = {... data, ...newKey}
        delete data[Object.keys(data)[i]]
    }
}
return data
};

console.log(mapOscarResults(data))

about 4 years ago · Santiago Gelvez Denunciar

0

This code works for your example:

const { Results: results, ...rest } = {
  "Name": "John",
  "Results": [
    {
      "Type": "DB",
      "Immediate_Action": "No",
    }
  ]
}

const res = {...rest, ...results.reduce((prev, curr) => ({
  ...prev,
  ...curr
}), {})}

console.log(res)

But I don't know what you expect when the Results array has more than one element.

In that condition, if this code does not fill your needs, ask me to change it.

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