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

128
Vistas
JS map array of objects to array of objects

I have an API that returns an array of objects(objects represent individual attributes) with individual keys. Objects can have different types and if the object is type 8 or 9 it means it's a multiselect. Multiselect objects look like this:

{
...
type:8
key: attribute6
multiselect_txt: Apple$Carrot$Plum
multiselect_id: 34$13$21
...
}

This would mean that on multiselect the user picked an Apple(id: 34), a Carrot(id: 13) and a Plum(id: 21). So, the text order aligns with the id order and the user picks are seperated by a dollar('$') sign.

My question is, how can I from an array of objects like this:

[
  {
   key: attribute1
   type: 8
   multiselect_txt: Apple$Carrot$Plum
   multiselect_id: 34$13$21
  },
  {
   key: attribute2
   type: 8
   multiselect_txt: Coke$Juice
   multiselect_id: 11$26
  }
]

make an array like this:

[{
  attribute1: [{
    name: 'Apple'
    id: 34
  }, {
    name: 'Carrot'
    id: 13
  }, {
    name: 'Plum'
    id: 21
  }],
}, {
  attribute2: [{
    name: 'Coke'
    id: 11
  }, {
    name: 'Juice'
    id: 26
  }]
}]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

const myTransform = (arr, sep) => (
  arr.map(
    ({
      key,
      multiselect_txt : tx,
      multiselect_id: ids
    }) => ({
      [key]: tx.split(sep).map(
        (name, idx) => ({
          name,
          id: +ids.split(sep)[idx]
        })
      )
    })
  )
);

/* EXPLANATION
// method to transform
// accepts "arr" array and "sep" separator string (like '$')
const myTransform = (arr, sep) => (
  // iterate over "arr"
  arr.map(
    ({
      // destructure to directly access props
      key,
      multiselect_txt : tx,   // rename props to "tx", "ids" for easy usage
      multiselect_id: ids
    }) => ({
      // transform to desired structure
      // key is the value stored in "key"
      // value array is constructed by using ".split()"
      [key]: tx.split(sep).map(
        // the text within "tx" is the name
        (name, idx) => ({
          name,
          // the number within "ids" at corresponding "idx" is the id
          id: +ids.split(sep)[idx]
        })
      )
    })    // implicit return from ".map()"
  )       // implicit return from "myTransform()" method
);
*/

const dataArr = [{
    key: 'attribute1',
    type: 8,
    multiselect_txt: 'Apple$Carrot$Plum',
    multiselect_id: '34$13$21'
  },
  {
    key: 'attribute2',
    type: 8,
    multiselect_txt: 'Coke$Juice',
    multiselect_id: '11$26'
  }
];

console.log(
  'transformed array to:\n',
  myTransform(dataArr, '$')
);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Inline comments added to the snippet above.

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