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

115
Vistas
how to push all value of an array into another array

For this kind of array:

 let combinazioniMat = [
     ["Person1", "Person2", "Person3", "Person4", "Person6"],
     ["Person1", "Person2", "Person3", "Person5", "Person6"],
     ["Person1", "Person2", "Person3", "Person6", "Person5"],
    ]

I want for each array to add each of the element of another array:

let mediciPom = ["Person1", "Person4"]

I tried to do like this, but it doesn't work...

let combinazioniTemp =[]

for (let combN=0; combN<combinazioniMat.length;combN++){
for (let med of mediciPom){
combinazioniMat[combN].push(med);
combinazioniTemp.push(combinazioniMat[combN]);
combinazioniMat[combN].pop()
}}

THIS IS THE RESULT I WANT TO OBTAIN (UPDATED):

    [
      ["Person1", "Person2", "Person3", "Person4", "Person6", "Person1" ],
      ["Person1", "Person2", "Person3", "Person4", "Person6", "Person4" ],
      ["Person1", "Person2", "Person3", "Person5", "Person6", "Person1" ], 
      ["Person1", "Person2", "Person3", "Person5", "Person6", "Person4" ], 
      ["Person1", "Person2", "Person3", "Person6", "Person5", "Person1" ],
      ["Person1", "Person2", "Person3", "Person6", "Person5", "Person4" ],
   ]

Moreover, I need the same function, but pushing the value only if not already present in the array To obtain this:

[
["Person1", "Person2", "Person3", "Person5", "Person6", "Person4" ],
["Person1", "Person2", "Person3", "Person6", "Person5", "Person4" ],
]

(Person1 will not be pushed because already present, and Person4 will be pushed only in the array in which it isn't)

Thanks and sorry if I was not clear before...

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

0

Well, that edit changes your question completely, so I'm posting a new answer.

So for the first part I'm assuming you want to replicate each subarray with a different value from mediciPom each time.

For that you can make use of the <Array>.flatMap function:

combinazioniMat.flatMap(arr => mediciPom.map(e => arr.concat(e)));

For the second part, you can just falsify the value when the element already exists, and then run the output array through a simple truth filter.

combinazioniMat.flatMap(arr => mediciPom.map(e => !arr.includes(e) && arr.concat(e))).filter(n=>n);

Full Example: withDuplicates is the first method, withoutDuplicates is the second:

let combinazioniMat = [
  ["Person1", "Person2", "Person3", "Person4", "Person6"],
  ["Person1", "Person2", "Person3", "Person5", "Person6"],
  ["Person1", "Person2", "Person3", "Person6", "Person5"],
]

let mediciPom = ['Person1', 'Person4'];

let withDuplicates = combinazioniMat.flatMap(arr => mediciPom.map(e => arr.concat(e)));
let withoutDuplicates = combinazioniMat.flatMap(arr => mediciPom.map(e => !arr.includes(e) && arr.concat(e))).filter(n=>n);

console.log('FIRST', withDuplicates)
console.log('SECOND', withoutDuplicates)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Find the remainder of the array index, and use that to grab the element from the mediciPom array.

const combinazioniMat=[["Person1","Person2","Person3","Person4","Person6"],["Person1","Person2","Person3","Person5","Person6"],["Person1","Person2","Person3","Person6","Person5"],["Person1","Person2","Person4","Person5","Person6"],["Person1","Person2","Person4","Person6","Person5"],["Person1","Person2","Person5","Person3","Person6"],["Person1","Person2","Person5","Person4","Person6"],["Person1","Person2","Person5","Person6","Person4"],["Person1","Person2","Person4","Person3","Person6"]];
const mediciPom = ['Person1', 'Person2'];

for (let i = 0; i < combinazioniMat.length; i++)  {
  const mp = mediciPom[i % 2];
  combinazioniMat[i].push(mp);
}

console.log(combinazioniMat);

about 4 years ago · Juan Pablo Isaza Denunciar

0

use this:

let combinazioniMat = [
  ["Person1", "Person2", "Person3", "Person4", "Person6"],
  ["Person1", "Person2", "Person3", "Person5", "Person6"],
  ["Person1", "Person2", "Person3", "Person6", "Person5"],
  ["Person1", "Person2", "Person4", "Person5", "Person6"],
  ["Person1", "Person2", "Person4", "Person6", "Person5"],
  ["Person1", "Person2", "Person5", "Person3", "Person6"],
  ["Person1", "Person2", "Person5", "Person4", "Person6"],
  ["Person1", "Person2", "Person5", "Person6", "Person4"],
  ["Person1", "Person2", "Person4", "Person3", "Person6"],
];

let mediciPom = ["Person1", "Person2"];

let combinazioniTemp = combinazioniMat.map((el, index) => [
  ...el,
  mediciPom[index % 2],
]);

console.log(combinazioniTemp);

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