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

196
Vistas
How to create another object from an existing object?

I want to create expectedObj from currentObj to be sent as response to the API. How can I do it?

The "value" and "label" will always have same values.

These are fixed set of keys. ignore_whitespace is not needed in expectedObj. No other keys are required to be removed.

    let currentObj =  {
          "partnerId": "1",
          "platform": {
              "label": "ADP",
              "value": "ADP"
          },
          "subPlatform": {
              "value": "Health",
              "label": "Health"
          },
          "activeIndicator": {
              "value": "Inactive",
              "label": "Inactive"
          },
          "partnerNotes": "",
          "ignore_whitespace": false
    }
   let expectedObj={
        "partnerId": "1",
        "platform": "ADP",
        "subPlatform": Health,
        "activeIndicator": "Inactive",
        "partnerNotes": ""
   }

What I have tried is this

for let (item in currentObj) {
 something
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Something like this should do what you're looking for:

const expectedObj = Object.entries(currentObj).reduce((a, [k, v]) => {
  if (v?.value !== undefined) a[k] = v.value;
  else a[k] = v;
  return a;
}, {});

Expand this code snippet for a working example:

const currentObj = {
  partnerId: '1',
  platform: {
    label: 'ADP',
    value: 'ADP',
  },
  subPlatform: {
    value: 'Health',
    label: 'Health',
  },
  activeIndicator: {
    value: 'Inactive',
    label: 'Inactive',
  },
  partnerNotes: '',
  ignore_whitespace: false,
};

const expectedObj = Object.entries(currentObj).reduce((a, [k, v]) => {
  if (v?.value !== undefined) a[k] = v.value;
  else a[k] = v;
  return a;
}, {});

console.log(expectedObj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This will do the transforms you need (as discussed in the comments, no more keys are expected, values are always the same as labels, etc):

let currentObj =  {
          "partnerId": "1",
          "platform": {
              "label": "ADP",
              "value": "ADP"
          },
          "subPlatform": {
              "value": "Health",
              "label": "Health"
          },
          "activeIndicator": {
              "value": "Inactive",
              "label": "Inactive"
          },
          "partnerNotes": "",
          "ignore_whitespace": false
    }
    
let expected = {
    ...currentObj,
    platform: currentObj.platform.value,
    subPlatform: currentObj.subPlatform.value,
    activeIndicator: currentObj.activeIndicator.value,
}

delete expected.ignore_whitespace;

console.log(expected);

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