挑戰是建立一張桌子。因此,第一個陣列如下所示:
[1001,235689,1002,235690,1003,235691]
第二個陣列是這樣的:
[
[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]
]
我怎樣才能將它們連接起來:
[
[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]
]
我已經嘗試遍歷第一個陣列并獲得第二個陣列,但是我迷失了重復第一個陣列的元素的次數與第二個陣列中的元素計數一樣多。
感謝任何幫助。
uj5u.com熱心網友回復:
您可以簡單地map()第二個陣列回傳由第一個的前兩個元素添加的每個陣列,這里預先創建一個slice()并將其傳播到地圖中的每個陣列中。
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(', ')));
uj5u.com熱心網友回復:
由于您為每一行使用 firstArray 中的兩個元素,因此您需要一次遍歷兩個元素,這會阻止使用大多數陣列方法,因此是一個很好的舊 for 回圈:
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);
uj5u.com熱心網友回復:
試試這個:
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);
}
}
uj5u.com熱心網友回復:
也許這個?
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)
uj5u.com熱心網友回復:
我想這就是你要問的
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
陣列.from ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418037.html
標籤:
上一篇:帶有物件的Python串列理解
下一篇:如何將文本的行和列重新排列為一列
