1

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)...

6
  • 1
    This structure is wrong, you should use combination of arrays and objects. There isn’t any “fancy” way to count in complicated arrays. Commented Mar 10, 2018 at 1:17
  • 1
    so one worker must have two row of records? Commented Mar 10, 2018 at 1:23
  • I edited the question @Sphinx Commented Mar 10, 2018 at 12:30
  • I edited the question @Akxe Commented Mar 10, 2018 at 14:43
  • 1
    The example array is not valid javascript. At least provide a valid starting point. It seems people want to help you, but you are not doing your part with this: Minimal, Complete, Verifiable Example! Commented Mar 12, 2018 at 1:36

3 Answers 3

2
+50

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answering @Markus Madeja
1

I am not quite what you are trying to achieve here, but here are a few questions for you to think about:

  1. Do mind sharing your code so that we can get a better idea on what you are trying to achieve here?
  2. Where are you retrieving this data? Are you provided with this 2D array as it is or did you create it? If you are the one who generated the array, then one way to optimize it is to use a different data structure. If the array is provided as it is, then you will have to loop through all the elements at least once (i.e. 15000 entries/row * number of rows) to convert it into a different data structure.
  3. How many times are you trying to count? Is this a one time off or a repeated task? If it's a repeated task, I would recommend caching the final data structure or treat it as a global value so that you do not need to compute the count every time you need it.

3 Comments

1. I edited the question and shared my code. 2. I think adding information will be enough for these questions @Jay
@Y.E.S. Can you share your loop that you used to count your workers? I'm not sure if I'm understanding the problem correctly -- it's pretty vague. What do you want your output to be? Are those worker IDs and department IDs given as input so that you know what to count, or do you want to count everything (i.e. for every worker, map department ids to the number of times they are being assigned to)?
as in the example that given you, I want to count every worker's how many times assigned to which department.
1

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]++;

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.