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

229
Visualizações
Merge multiple arrays based on their index using javascript

I need to merge two arrays into a single array. I have code but it is not working as expected-- it is merging them one after another, but I need to interlock the values.

<html>

<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
</head>

<body>
    <div id="result"></div>
    <script type="text/javascript">
    var array1 = [1, 2, 3, 4];
    var array2 = ["a", "b", "c", "d"];
    var newArray = $.merge(array1, array2);
    $("#result").html(newArray);
    //the result its 1234absd
    //ineed result like 1a,2b,3c,4d
    </script>
</body>

</html>

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use Array#map in plain javascript.

var array1 = [1, 2, 3, 4];
var array2 = ["a", "b", "c", "d"];

var newArray = array1.map((e, i) => e + array2[i]);
console.log(newArray);

If you use map on array1 then first parameter is current value of array1 in loop and the second parameter is index of that element in array. So you can match current element in array1 with elements from other arrays with the same index using index.

var array1 = [1, 2, 3, 4];
var array2 = ["a", "b", "c", "d"];
var array3 = ["f", "d", "s", "a"];

var newArray = array1.map(function(value, index) {
  return value + array2[index] + array3[index];
});
console.log(newArray);

about 4 years ago · Santiago Trujillo Relatório

0

You could use Array#reduce and Array#map for an arbitrary count of arrays with same length.

var a1 = [1, 2, 3, 4],
    a2 = ["a", "b", "c", "d"],
    a3 = [9, 8, 7, 6],
    a4 = ["z", "y", "x", "w"],
    result = [a1, a2, a3, a4].reduce((a, b) => a.map((v, i) => v + b[i]));

console.log(result);

ES5

var a1 = [1, 2, 3, 4],
    a2 = ["a", "b", "c", "d"],
    a3 = [9, 8, 7, 6],
    a4 = ["z", "y", "x", "w"],
    result = [a1, a2, a3, a4].reduce(function (a, b) {
        return a.map(function (v, i) {
            return v + b[i];
        });
    });

console.log(result);

about 4 years ago · Santiago Trujillo Relatório

0

Just a little variation note, to merge by index in an array multiple, to create an associative array of values.

const a = [1, 2, 3, 4],
      b = [34, 54, 54, 43]

console.log(
  a.map((e,i) => [e,b[i]])
)


Then later, to search for the closest match from a given value. On this example, searching for the best associated value for 3.7:

const a = [1, 2, 3, 4],
      b = [34, 54, 54, 43],
      c = a.map((e,i) => [e,b[i]]),
      search = 3.7,
      temp = [];
      
c.forEach(function(e){
  temp.push(e[0])
})

const seek = temp.reduce((m, n) => (Math.abs(n-search) < Math.abs(m-search) ? n : m))

const index = c.findIndex((s) => s.indexOf(seek) !== -1)
console.log(c[index][1])

This is not exactly how behave an object.

For example, identical index entries aren't merged but are holding their associated values in a new entry (duplicate index), we can roll Math from the index, and create transformation matrix. We can use any column as index and search both way.

about 4 years ago · Santiago Trujillo 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