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

107
Vistas
how to convert a items in a object to list of array
{
    Type: "Fire",
    Name: "Shark",
    StartRange0: "10",
    EndRange0: "20",
    StartRange1: "30",
    EndRange1: "40",
    StartRange2: "60",
    EndRange2: "70"
}

In the above object, StartRange and EndRange can be more items in different objects. i.e those values are obtained from a loop( StartRange + index), so there maybe StartRange1,2,3... lly, EndRange1,2,3...

The above object structure must be changed to,

{
   "Fire":{
      "Name":"Shark",
      "List":[
         [
            "10",
            "20"
         ],
         [
            "30",
            "40"
         ],
         [
            "60",
            "70"
         ]
      ]
   }
}

The List array inside the object is combination of [StartRange0,EndRange0],[StartRange1,EndRange2]... So on...

I tried,

let newObj = {}
newObj[oldObj.Type] = {
    Name: oldObj.Name, //oldObj is the first object defined above.
    List: [[oldObj.StartRange0,oldObj.EndRange0],[oldObj.tartRange1,oldObj.EndRange1],[oldObj.StartRange2,oldObj.EndRange2]]
}

But here, it is limited to StartRange2 and EndRange2 but I need it for StartRangeN and EndRangeN.

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

0

This is how I would have done map it

const input = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70"
}

const toList = (l) => Array.from({
  length: Object.keys(l).length / 2
}, (_, N) => [l[`StartRange${N}`], l[`EndRange${N}`]])

const toOutput = ({ Type, Name, ...rest }) => {
  const List = toList(rest)
  return ({
    [Type]: {
      Name,
      List
    }
  })
}

const output = toOutput(input)
console.log(output)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can play around with this example

const object = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70"
};

let index = 0;
let isFinished = false;
const results = [];

while (!isFinished) {
  const startKey = `StartRange${index}`;
  const endKey = `EndRange${index}`;

  isFinished = !object.hasOwnProperty(startKey) || !object.hasOwnProperty(endKey);

  !isFinished && results.push([object[startKey], object[endKey]]);

  index++;
}

console.log(results);
about 4 years ago · Juan Pablo Isaza Denunciar

0

This may work but it would be better if you could change that structure and use another one.

const test = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70",
};

const format = (obj) => {
  const { Type, Name, ...rest } = obj;
  const list = [];
  const keys = Object.keys(rest);
  for (let index = 0; index < keys.length; index++) {
    const start = rest[`StartRange${index}`];
    const end = rest[`EndRange${index}`];
    if (start && end) {
      list.push([start, end]);
    } else {
      break; // assuming you don't have any other elements
    }
  }
  return {
    [Type]: {
      Name,
      List: list,
    },
  };
};

console.log(JSON.stringify(format(test)));
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