[英]Comparing & getting values from two arrays in google script
這是輸入,
colA = [1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3];
colB = [1.1 ,1.1 ,1.2 ,1.3, 1.3, 'ab', 2.1, 2.1, 2.2, 2.2, 'ab', 3.1, 3.2, 3.3, 3.3, 3.3, 'ab'];
我希望控制台中的 output 看起來像:
1, 1.1, count of 1.1 = 2
1, 1.2, count of 1.2 = 3
1, 1.3, count of 1.3 = 2
1, AB, count of AB = 1
2, 2.1, count of 2.1 = 2
2, 2.2, count of 2.2 = 2
2, AB, count of AB = 2
.
.
.
//or something like this where I must get these three values.
代碼:
for(i=0; i < colA.length; i++)
{
for(j=0; j < colB.length; j++)
{
// what to do here to compare the values.
}
}
我是否應該使用兩個 for 循環 go 因為我希望代碼得到真正優化,因為將有大約 10k 行數據。 接受任何建議。 謝謝您的幫助。
您可以使用單個循環進行迭代,並使用后繼和遞增count
檢查值。
如果不相等,則使 output 並將計數重置為 1。
var colA = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3], colB = [1.1, 1.1, 1.2, 1.3, 1.3, 'ab', 2.1, 2.1, 2.2, 2.2, 'ab', 3.1, 3.2, 3.3, 3.3, 3.3, 'ab'], count = 1, i, length = colA.length; for (i = 0; i < length; i++) { if (colB[i] === colB[i + 1]) { count++; continue; } console.log(colA[i], colB[i], count); count = 1; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.