0

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

10
  • what is this all for? Commented Jan 23, 2022 at 14:32
  • const targetArr = combinazioniMat.map(x => [...x, ...mediciPom]); <--- please try this and share your feedback. This will add all elements of mediciPom into each array within combinazioniMat. Commented Jan 23, 2022 at 14:36
  • 1
    @Damiano your question contradicts that --- your desired output has dupicate values Commented Jan 23, 2022 at 15:12
  • 1
    @jsN00b It doesn't work. It add both the value at the end. I need one value. And repeating with each value... Commented Jan 23, 2022 at 17:09
  • 1
    @Damiano well can you update your question and explain what you want? just add an example output for the additional requirement Commented Jan 23, 2022 at 17:15

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

It doesn't work. It add alternately one value, I need to add each value for each array... for example ["Person1", "Person2", "Person3", "Person4", "Person6", "Person1"], ["Person1", "Person2", "Person3", "Person5", "Person6", "Person2"],
This is exactly what you asked for. How is what you need any different from my answer? I don't understand. @Damiano
Sorry, I updated the sample because I realize it wasn't clear...
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);

3 Comments

It doesn't work. It add alternately one value, I need to add each value for each array... for example ["Person1", "Person2", "Person3", "Person4", "Person6", "Person1"], ["Person1", "Person2", "Person3", "Person5", "Person6", "Person2"],
this will work: let combinazioniTemp = combinazioniMat.map((el, index) => [ ...el, mediciPom[index], ]);
Sorry, maybe I wasn't clear, I hope I cleared in the updated question. The code you wrote doesn't works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.