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

83
Vistas
combine array elements and send response

This is my request body which I am sending on click of form submit button:

{ 
    "name":'test', 
    "age":'test1', 
    "appId":[10,20,30], 
    "dataId":[1,2,3] 
}

I want to modify it this this:

[
    { name: "test", age: "test1", appId: 10, dataId: 1 },
    { name: "test", age: "test1", appId: 10, dataId: 2 },
    { name: "test", age: "test1", appId: 10, dataId: 3 },
    { name: "test", age: "test1", appId: 20, dataId: 1 },
    { name: "test", age: "test1", appId: 20, dataId: 2 },
    { name: "test", age: "test1", appId: 20, dataId: 3 },
]

This needs to be done using Javascript (ES6).

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

0

This is a Cartesian product:

const cartesian = (a, b) => a.flatMap(appid => b.map(dataid => ({appid, dataid})));

console.log(cartesian(["a", "b", "c"], [1, 2, 3]));

In a comment below you provide an object wrapper around the two input arrays, and want the other object properties to be copied into the result objects.

So then it becomes:

const cartesian = (obj, a, b) => 
    obj[a].flatMap(x => obj[b].map(y => ({...obj, [a]: x, [b]: y}) ));


const response = {name:'test', age:'test1', appId:[10,20,30], dataId:[1,2,3]};
const result = cartesian(response, "appId", "dataId");
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

let combinedArray = [];
array1.forEach((element, index) => {
    let batch = [];
    array2.forEach((element2, index2) => {
         batch.push({
              appid: element,
              dataid: element2
         });
    });
    combinedArray.push(...batch);
});

Something along these lines. Seems like the easiest way to do this assuming arrays are the same length and in the right order. You can use for loop instead.

Actually, that doesn't seem like the right solution either. I'm not sure what pattern you are trying to achieve with your example, so I will leave it like this. You can adjust the algorithm to your needs.

Edited because I re-read the question.

about 4 years ago · Juan Pablo Isaza Denunciar

0

[].concat.apply([],[1,2,3].map(x => [1,2,3].map(y=> { return {'a': x, 'b':y}})))
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