How to combine arrays and repeat first array's elments as many times as the length of the second array?
Solution 1:
You can simply map()
the second array returning each array prepended by the first two elements of the first, here creating a single slice()
beforehand and spreading it into each array in the map.
const arr1 = [1001, 235689, 1002, 235690, 1003, 235691];
const arr2 = [['tecido', 1, 'Crepe Georgette', , , , 'mt', 0.9, 11.9, 10.71], ['tecido', 2, 'Forro', , , , 'kg', 0.5, 7.916666666666667, 3.9583333333333335,], ['aviamento', 5, 'Tag Láureen Instrução Lavagem', , , , 'und', 1, 0.5, 0.5], ['aviamento', 3, 'Entretela malha', , , , 'und', 1, 10, 10],];
const prepend = arr1.slice(0, 2);
const result = arr2.map((arr) => [...prepend, ...arr]);
result.forEach(a => console.log(a.join(', ')));
Solution 2:
Since you are using two elements from the firstArray for each row, you'll need to iterate through it two at a time, which prevents use of most array methods, so a good old for loop:
let output = [];
for (let i=0; i < firstArray.length; i+=2) {
secondArray.forEach(el => {
output.push([firstArray[i], firstArray[i + 1], ...el]);
});
}
console.log(output);