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

176
Vistas
Javascript join not working on created array

Strange issue on join. It works on initial array, but not with filtered array. Tested also functions for string, but resultA is not string also.

What is wrong with the code:

const constants = [2, 2, 4, 2, 6, 4, 7, 8];
const stdFound = [];

// Makes list of indexes - positions from matching value 2 in constants
constants.filter(function(elem, index, array) {
  if (elem == (2)) {
    stdFound.push(index);
  }
});

//Makes new array from results by indexes found
const resultA = [constants.filter((x, i) => stdFound.includes(i))];
document.getElementById("resultA").innerHTML = resultA;
//Makes string by join
document.getElementById("resultJ").innerHTML = resultA.join(" x ");
<p> ARRAY : <span id="resultA"></span>
  <p> JOIN x : <span id="resultJ"></span></p>

Right now, this code results: ARRAY : 2,2,2 JOIN x : 2,2,2

so clearly, join is not working.

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

0

filter() already returns an array. No need to wrap in this [].

console.log(resultA) will show that.

const constants = [2, 2, 4, 2, 6, 4, 7, 8];
const stdFound = [];

// Makes list of indexes - positions from matching value 2 in constants
constants.filter(function(elem, index, array) {
  if (elem == (2)) {
    stdFound.push(index);
  }
});

//Makes new array from results by indexes found
const resultA = constants.filter((x, i) => stdFound.includes(i));
document.getElementById("resultA").innerHTML = resultA;

//Makes string by join
document.getElementById("resultJ").innerHTML = resultA.join(" x ");
<p> ARRAY : <span id="resultA"></span>
  <p> JOIN x : <span id="resultJ"></span></p>

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