[英]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.