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

228
Vistas
Return new JS object using previous object values

I have an object of objects (for example):

const previousMap = {123456: {title: 'Test 1', productID: 123456}, 234567: {title: 'Test 2', productID: 234567}}

and would like to return a new object, with each key as the titles of the previous object, and its values as the sku (which also happen to be the keys of the last object-- but I would rather use the .sku attribute).

For example:

const newMap = {'Test 1' : 123456, 'Test 2': 234567}

I have tried:

newMap = Object.entries(previousMap).map(([key, value]) => ({[value.title] : value.productID}))

but wind up getting an array of objects.

This is something that I feel as though I would normally be able to accomplish but my brain is really not working today. Any help = greatly appreciated :)

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

0

Another possible solution:
Changing the map to return an array of key and value [k,v] and then calling Object.fromEntries on it.

const previousMap = {
  123456: {
    title: 'Test 1',
    productID: 123456
  },
  234567: {
    title: 'Test 2',
    productID: 234567
  }
}

let newMap = Object.fromEntries(
  Object.entries(previousMap)
  .map(([key, value]) => [value.title, value.productID])
);

console.log(newMap)

about 4 years ago · Juan Pablo Isaza Denunciar

0

A .map will, by definition, always return an array. Instead you can use .reduce, starting with an empty object as the initial value for your accumulator, and spread it into the new return object at each iteration:

const previousMap = {
  123456: {
    title: 'Test 1',
    productID: 123456
  },
  234567: {
    title: 'Test 2',
    productID: 234567
  }
}

const newMap = Object.entries(previousMap)
  .reduce(
    (acc, [key, value]) => ({
      ...acc,
      [value.title]: value.productID
    }), {}
  );

console.log(newMap);

You could also just accomplish this with a plain loop and keep mutating a single object instead, if you desired.

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