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

179
Visualizações
How to combine 2 arrays into 1 React

I am pretty new to react. Currently i am trying to combine 2 arrays item into one but i would like to iterate over each item. How can i do that?

const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },]

const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },]

Expected result: ["red,s","red,m","red,l"],["yellow,s","yellow,m","yellow,l"]....

I have tried concat() & reduce, but the result i get is two array merge together rather than items merge seperately. Can someone help please?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use map to manipulate with data.

const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },]

const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },]

console.log(...arr1.map(data => {

return arr2.map(data2 => data.value + "," + data2.value)
}))

about 4 years ago · Juan Pablo Isaza Relatório

0

This worked for me, using nested loops:

const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },]
const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },]

const newarr = []
for (i=0; i<arr1.length; i++) {
    tmp = []
    for (j=0; j<arr2.length; j++) {
        tmp.push([arr1[i].value, arr2[j].value])
    }
    newarr.push(tmp)
}

console.log(newarr)

// [
//   [ [ 'red', 's' ], [ 'red', 'm' ], [ 'red', 'l' ] ],
//   [ [ 'yellow', 's' ], [ 'yellow', 'm' ], [ 'yellow', 'l' ] ],
//   [ [ 'Blue', 's' ], [ 'Blue', 'm' ], [ 'Blue', 'l' ] ]
// ]
about 4 years ago · Juan Pablo Isaza Relatório

0

Give this a try (feel free to change out the keys to either .value or .label)

function mergeArrays(arr1, arr2) {
    let result = [];
    
    for (let i = 0; i < arr1.length; i++) {
        result.push([]);
        for (let j = 0; j < arr2.length; j++) {
            result[i] = [ ...result[i], arr1[i].value + arr2[j].value ]
        }
    }
}

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