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

98
Vistas
Transform complex object into key value

I have an array

const arr = [
  {
    "id": 1,
    "name": "Bitcoin",
    "symbol": "BTC",
    "slug": "bitcoin",
    "rank": 1,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:21.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  },
  {
    "id": 2,
    "name": "Litecoin",
    "symbol": "LTC",
    "slug": "litecoin",
    "rank": 20,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:22.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  }
]

And I want to transform the array to this

{
  "BTC": "Bitcoin",
  "LTC": "Litecoin",
}

Is there a better way than this?

const result = {}
arr.reduce((accum, val) => {
  Object.assign(result, { [val.symbol]: val.name });
}, {})

console.log(result)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Use Object.entries() an each object which will return them as an array of arrays -- each sub-array will be a key/value pair ([key, value]) then use Object.assign() to create a new object ({[key]: value}) to return. Then flatten it so they are all in one array.

const arr=[{id:1,name:"Bitcoin",symbol:"BTC",slug:"bitcoin",rank:1,is_active:1,first_historical_data:"2013-04-28T18:47:21.000Z",last_historical_data:"2022-02-18T11:39:00.000Z",platform:null},{id:2,name:"Litecoin",symbol:"LTC",slug:"litecoin",rank:20,is_active:1,first_historical_data:"2013-04-28T18:47:22.000Z",last_historical_data:"2022-02-18T11:39:00.000Z",platform:null}];

const conv = array => {
 let objects = array.map(obj => Object.entries(obj).map(([key, val]) => Object.assign({}, {[key]: val})));
 return objects.flat();
};

console.log(conv(arr));

about 4 years ago · Juan Pablo Isaza Denunciar

0

let arr = [
  {
    "id": 1,
    "name": "Bitcoin",
    "symbol": "BTC",
    "slug": "bitcoin",
    "rank": 1,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:21.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  },
  {
    "id": 2,
    "name": "Litecoin",
    "symbol": "LTC",
    "slug": "litecoin",
    "rank": 20,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:22.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  }
]

// As suggested, here it is without having to create an initial array

let alternativeArray = arr.map((val) => {
  return {[val.symbol]: val.slug}
})

console.log(alternativeArray)

Here is the answer to why the object key is being set in that format: Javascript set object key by variable

about 4 years ago · Juan Pablo Isaza Denunciar

0

The answer is

Object.fromEntries(arr.map(({symbol, name}) => [symbol, name]))
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