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

127
Vistas
New Map from JSON data with unique values

I'm trying to create a new map of [key:value] as [trait_type]:[{values}] So taking the below data it should look like:


    [Hair -> {"Brown", "White-Blue"}, 
     Eyes -> {"Green", "Red"}
    ]

Heres my json obj

    [
        {
          "name":"Charlie",
          "lastname":"Gareth",
          "date":1645462396133,
          "attributes":[
             {
                "trait_type":"Hair",
                "value":"Brown"
             },
             {
                "trait_type":"Eyes",
                "value":"Red"
             }
          ]
        },
        {
          "name":"Bob",
          "lastname":"James",
          "date":1645462396131,
          "attributes":[
             {
                "trait_type":"Hair",
                "value":"White-Blue"
             },
             {
                "trait_type":"Eyes",
                "value":"green"
             }
          ]
        }
        ];

To note: I'm also trying to make it the values unique so If I have 2 people with red eyes the value would only ever appear once.

This is what I have so far, kind of works but in the console window the values come out as a char array.


    let newData = data.map((item) =>
        item.attributes.map((ats) => {
            return { [ats.trait_type]: [...ats.value] };
        })
    );
    newData.map((newItem) => console.log(newItem));

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

0

You could take an object and iterate the nested data with a check for seen values.

const
    data = [{ name: "Charlie", lastname: "Gareth", date: 1645462396133, attributes: [{ trait_type: "Hair", value: "Brown" }, { trait_type: "Eyes", value: "Red" }] }, { name: "Bob", lastname: "James", date: 1645462396131, attributes: [{ trait_type: "Hair", value: "White-Blue" }, { trait_type: "Eyes", value: "green" }] }],
    result = data.reduce((r, { attributes }) => {
        attributes.forEach(({ trait_type, value }) => {
            r[trait_type] ??= [];
            if (!r[trait_type].includes(value)) r[trait_type].push(value);
        })
        return r;
    }, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

try this

const extractTrait = data =>
  data.flatMap(d => d.attributes)
  .reduce((res, {
      trait_type,
      value
    }) => {
      return {
        ...res,
        [trait_type]: [...new Set([...(res[trait_type] || []), value])]
    }
  }, {})


const data = [{
    "name": "Charlie",
    "lastname": "Gareth",
    "date": 1645462396133,
    "attributes": [{
        "trait_type": "Hair",
        "value": "Brown"
      },
      {
        "trait_type": "Eyes",
        "value": "Red"
      }
    ]
  },
  {
    "name": "Bob",
    "lastname": "James",
    "date": 1645462396131,
    "attributes": [{
        "trait_type": "Hair",
        "value": "White-Blue"
      },
      {
        "trait_type": "Eyes",
        "value": "green"
      }
    ]
  }
];

console.log(extractTrait(data))

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