Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!