Reputation: 764
I have a large string array like:
var array = [["worker ", "department", "2", "6", "8", "3", "1", "12", ... , 14]
["1", "Duty/Not", "1", "0", "1", "0", "0", "1", ..., "0"]
["worker ", "department", "11", "10", "11", "3", ..., "7"]
["2", "Duty/Not", "0", "1", "1", "1", "1", "1", ..., "1"]];
In this array, every array of array row has got 15000 elements and there is a relation between first two subarrays and others. For example, the first row contains the first worker's department where s/he works. Otherwise, the second row contains the first worker's duty or not represent with 1 or 0.
I want to count worker's how many times be assigned to which department.
I tried to count using a loop. However, it takes a long time.
Edit:
A part of my implementation
//Reading and parsing from csv file
d3.text("worker.csv", function(text) {
var data = d3.csvParseRows(text).map(function(row) {
return row.map(function(string) {
return string;
});
});
//Concatination
var concatArray = [].concat(...data);
//console.log(concatArray);
// Remove NULL from array
var filteredAry = concatArray.filter(function(e) { return e != ""});
console.log(filteredAry);
//convert array row based
var row = [], index = 0;
for(var i = 0; i < element; i++){
row[i]=filteredAry.filter(word => {index=
filteredAry.indexOf(word,index)+1;return ((index-1) % element) == i;});
console.log(row[i]);
}
Edit 2:
I convert to array column based. The output of array is like
["worker", "1"]
["depermant", "Duty/Not"]
["2", "1"]
["2", "1"]
["8", "1"]
["3", "0"]
["1", "0"]
["12", "1"]
["10", "1"]
["5", "0"]
["14", "1"]
["4", "1"]
["2", "1"]
["4", "1"]
["6", "1"]
["6", "0"]
["6", "0"]
["3", "1"]
Now, my question is almost same as before. I want to map department ids to the number of times they are being assigned, for every worker. For example, expected output is like
output = (1, 0),(2, 3), (3, 1), (4, 2), (5, 0), (6, 1)...
Upvotes: 1
Views: 302
Reputation: 856
It's already solved but a solution for you original problem that should be somewhat fast would be:
// Testdata
var data = [["worker ", "department", "2", "6", "8", "3", "1", "12", 11],
["1", "Duty/Not", "1", "0", "1", "0", "0", "1", "0"],
["worker ", "department", "11", "10", "11", "3", "2", "3", 11],
["2", "Duty/Not", "0", "1", "1", "1", "1", "1", "1"]];
// We need to know the number of departments upfront for speedup
const numOfDepartments = 12;
// First there are no worker in the departments
let countForEachDepartment = Array(numOfDepartments).fill(0);
let referrenceToDepartmentsInRow;
// go through each row
data.forEach((row, index) => {
// always remove the first 2 columns because they are useless
row.shift();
row.shift();
// are we in an odd or even row?
if (index % 2) {
// even row: we calc the numbers
row.forEach((column, index) => {
// the array start at one and we convert the string to a number with -0 to simply add it
countForEachDepartment[referrenceToDepartmentsInRow[index] - 1] += (column-0);
})
} else {
// odd row: we have the number to the departments that will work as a reference for even rows
referrenceToDepartmentsInRow = row;
}
});
console.log(countForEachDepartment);
Upvotes: 2
Reputation: 764
I solved the problem.
Solution:
var count = [], i, value, total = 0;
for (i = 0; i < array.length; i++) {
value = array[i];
if (typeof count[value] === "undefined") {
count[value] = 1;
} else {
count[value]++;
}
}
Upvotes: 1
Reputation: 412
I am not quite what you are trying to achieve here, but here are a few questions for you to think about:
15000 entries/row * number of rows
) to convert it into a different data structure.Upvotes: 1