The challenge is to build a table. So, the first array is like to the following:
[1001,235689,1002,235690,1003,235691]
The second array is like this:
[
[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]
]
How can I concatenate them to be like:
[
[1001,235689,tecido,1,Crepe Georgette,,,,mt,0.9,11.9,10.71],
[1001,235689,tecido,2,Forro,,,,kg,0.5,7.916666666666667,3.9583333333333335],
[1001,235689,aviamento,5,Tag Láureen Instrução Lavagem,,,,und,1,0.5,0.5],
[1001,235689,aviamento,3,Entretela malha,,,,und,1,10,10]
]
I've tried looping over the first array and get the second in, but I get lost repeating the first array's elements as many times as the elements count in the second.
Appreciate any help.
CodePudding user response:
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(', ')));
CodePudding user response:
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);
CodePudding user response:
Try this:
function test_07() {
try {
var a = [1001,235689,1002,235690,1003,235691];
var b = [["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]];
var c = [];
for( var i=0; i<b.length; i ) {
c.push(a.concat(b[i]));
}
console.log(c)
}
catch(err) {
console.log(err);
}
}
CodePudding user response:
Maybe this one?
let arr1 = [1001,235689,1002,235690,1003,235691]
let 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]];
let arr3 = []
for (let i = 0;i<arr1.length;i =2){
for(let y in arr2){
let little = [arr1[i],arr1[i 1]]
arr3.push(little.concat(arr2[y]))
}
}
console.log(arr3)
CodePudding user response:
I think this is what you are asking
function myfunk1() {
const a = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];//create a table with row length of a
const g = ['a', 'b', 'c', 'd'];
const h = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h','i'];
let b = [];
b.push(a);
Logger.log('With Just A: %s', JSON.stringify(b));
let c = [...Array.from(new Array(a.length).keys(), x => x)];//creates another array of length a
b.push(c);
Logger.log('With a and c: %s', JSON.stringify(b));
let d = [...Array.from(new Array(a.length).keys(), x => '')];//creates another array of length a
b.push(d);
Logger.log('With a and c and d: %s', JSON.stringify(b));
let e = [...Array.from(new Array(a.length).keys(), x => g[x] ? g[x] : '')];//creates another array of length a
b.push(e);
Logger.log('With a and c,d and e: %s', JSON.stringify(b));
let f = [...Array.from(new Array(a.length).keys(), x => h[x] ? h[x] : '')];//creates another array of length a
b.push(f);
Logger.log('With a and c,d and e: %s', JSON.stringify(b));
}
Execution log
2:29:57 PM Notice Execution started
2:29:56 PM Info With Just A: [["a","b","c","d","e","f","g"]]
2:29:56 PM Info With a and c: [["a","b","c","d","e","f","g"],[0,1,2,3,4,5,6]]
2:29:56 PM Info With a and c and d: [["a","b","c","d","e","f","g"],[0,1,2,3,4,5,6],["","","","","","",""]]
2:29:56 PM Info With a and c,d and e: [["a","b","c","d","e","f","g"],[0,1,2,3,4,5,6],["","","","","","",""],["a","b","c","d","","",""]]
2:29:56 PM Info With a and c,d,e and f: [["a","b","c","d","e","f","g"],[0,1,2,3,4,5,6],["","","","","","",""],["a","b","c","d","","",""],["a","b","c","d","e","f","g"]]
2:29:58 PM Notice Execution completed
