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

118
Vistas
How to add key and values to anobject by splitting a string inside the object

I have the following array:

[
  {
    “Key”: “CUST”,
    “Segment”: “A;B;C;D;E”
  }
]

I want to transform it to:

[
  {
    “Key”: “CUST”,
    “Segment 1”: “A”,
    “Segment 2”: “B”,
    “Segment 3”: “C”,
    “Segment 4”: “D”,
    “Segment 5”: “E”
  }
]

I need to dynamically create the key names based on how many ";" separated values I have in the string “Segment,” as that number can change. I know I will probably need to use Split and maybe Reduce, Map, and Extend, but I’m having a tough time figuring it out. Can someone help me with this? Thank you!

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

0

Thank you all for your possible solutions! I was actually able to achieve what I was looking to do using the following code:

$.extend($[‘Customer Security Segment’].split(";").reduce((accum, curVal, index) => accum.extend({["Customer Security Segment " + (index + 1)]: curVal }), {}))

This would give me the new columns I was looking for. After that I could simply drop the orignal column for "Customer Security Segment.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can achieve this by using Array.map() :

// Input array
const arr = [
  {
    Key: "CUST",
    Segment: "A;B;C;D;E"
  }
];

const res = arr.map((obj) => {
  const resObj = {
    Key: obj.Key
  };
  obj.Segment.split(';').forEach((item, index) => {
    resObj['Segment ' + (index + 1)] = item
  });
    return resObj;
});

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use split as you thought with forEach

object.forEach(item => {
  item["Customer Security Segment"].split(";").forEach((segment, idx) => {
    item[`Customer Security Segment ${idx + 1}`] = segment;
  });
  delete item["Customer Security Segment"]
});
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