Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda