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

479
Vistas
How to swap Object keys/values into Array?

I have a large Object containing addresses and numbers.

{
  '0x2bac18ad331A3137AbEC3a029dBb3A6cC25835ea': 1,
  '0x22Bf3f4EA7862739024C122aDDd2FB2981c076a7': 3,
  '0x9E6E8b584F26503C84661674dCD7821099c8a51d': 1,
  '0x37D74ca0F842817C6FC4Ea267cF2d49DEa0C06a8': 1,
  '0xDf92913902087aD0Bfac39659B60CebE1100595a': 1
// ...
}

and I try to swap key/values and store all addresses with the same number value in an Array, thinking of something like this:

var newObj = {
 "1": [addr1, addr2, addr3],
 "2": [addr1, addr2, addr3],
 "3": [addr1, addr2, addr3]
//...
}

I found a way to swap like this:

function swaptoArray(json){
    var ret = {};
    for(var key in json){
    ret[json[key]] = key;
    }
    return ret;
  }

but I am having trouble making the value-part an Array containing many addresses. I tried this, but don't understand the wrong result. Any hints?

function swaptoArray(json){
    var ret = {};
    var newArray = [];

    for(var key in json){

      ret[json[key]] = newArray.push(key);

    }
    return ret;
  }

Result:

{ '1': 5, '3': 2 }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need an array as result for every value as key for the result.

function group(object) {
    const result = {};
    for (const key in object) (result[data[key]] ??= []).push(key);
    return result;
}

const data = {
  '0x2bac18ad331A3137AbEC3a029dBb3A6cC25835ea': 1,
  '0x22Bf3f4EA7862739024C122aDDd2FB2981c076a7': 3,
  '0x9E6E8b584F26503C84661674dCD7821099c8a51d': 1,
  '0x37D74ca0F842817C6FC4Ea267cF2d49DEa0C06a8': 1,
  '0xDf92913902087aD0Bfac39659B60CebE1100595a': 1
}

console.log(group(data));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's a solution using Object.entries:

const obj = {
  '0x2bac18ad331A3137AbEC3a029dBb3A6cC25835ea': 1,
  '0x22Bf3f4EA7862739024C122aDDd2FB2981c076a7': 3,
  '0x9E6E8b584F26503C84661674dCD7821099c8a51d': 1,
  '0x37D74ca0F842817C6FC4Ea267cF2d49DEa0C06a8': 1,
  '0xDf92913902087aD0Bfac39659B60CebE1100595a': 1
}

const objArr = Object.entries(obj)

function getArray(key) {
  let ret = []
  objArr.forEach(item => {
    if(item[1] === key) ret.push(item[0])
  })  
  return ret
}

function swaptoArray(json) {
  let ret = {}
  for (let key in json) {
    const newKey = json[key]
    ret[newKey] = getArray(newKey)
  }
  return ret
}

console.log(swaptoArray(obj))

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