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

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

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

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

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 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