Reputation:
Here is the input,
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'];
I want the output in the console to look like:
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.
Code:
for(i=0; i < colA.length; i++)
{
for(j=0; j < colB.length; j++)
{
// what to do here to compare the values.
}
}
Should I go with two for loops because I want the code to be really optimized as there is going to be around 10k rows of data. Open to any suggestions. Thanks for the help.
Upvotes: 2
Views: 79
Reputation: 386600
You could iterate with a single loop and check the value with the successor and increment count
.
If unequal make the output and reset count to one.
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;
}
Upvotes: 2