Multidimensional Array sort map ... just struggling

i’m really lost :frowning:

This is the array structure i got:
Array [ Array[17], Array[17], Array[17], Array[17], Array[17], Array[17], Array[17], Array[17], Array[17], Array[17], 13484 more… ]

This is generated out of a .csv file. So for every line an array with the same structure.
Where on index [9] is a serialnumber, at position[10] a CODE and on [13] is a date as string (“”).
Serialnumber is sometimes number, sometimes string (mixed numbers and letters)

I would like to put all entries with the same serialnumber into one array. After sorting by creating that new array the next task is to check for the most recent date for all entries where the CODE [10] is eqal and write that again into an new array.

Now i should be left with the same structured arrays as of the beginning, just less data.
Last step would be to take each array and check again for [10] and [13]. Now select the oldest date for every [10]
and grab the content out of [14] (meter-read) and put it into an array.

The final Array should still have the same structure, just added more values in between started at [15].

Array [ Array[19], Array[19], Array[19], Array[19],…]

Hope somebody is willing to help me out here.

Check the following.I am using concat() function.

var arrToConvert = [[0,0,1],[2,3,3],[4,4,5]];
var newArr = [];

for(var i = 0; i < arrToConvert.length; i++)
    newArr = newArr.concat(arrToConvert[i]);


No. Sorry, this will kill your browser on big data :slight_smile:
And using arithmetic looping will create ghosts. You can prevent that by using “for in”.

Maybe somebody else can help…

 var csv = new CSV(contents).parse();
 var testArray = csv.slice(0);
 testArray.sort(function(a,b){return a[9] - b[9];});

that’s just how far i am :frowning:

maybe a bit more visual what i am trying to do

sort by serial number [9] is working as far as its sorting …


[ [0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN], and so on]

what i would like to have is

[[ [0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN], aso]],[[0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN],[0,1,2,3,4,5,6,7,8,SN]] ]

So you want to reduce the CSV data to sub-arrays, grouped by the entry for the serial number? This could certainly be done, but I think such array-juggling can quickly get out of hand; maybe a plain object would be a more suitable data structure here, where the keys are the serial numbers and the values the corresponding entries. Along the lines of

const data = csv.reduce((result, current) => {
  const serialNumber = current[9]

  result[serialNumber] = result[serialNumber] || []

  return result
}, {})

or, in a more reusable way

function groupByValue(array, index) {
  return array.reduce((result, current) => {
    const value = current[index]

    result[value] = result[value] || []

    return result
  }, {})

const data = groupByValue(csv, 9)

You can then get the entries for a given serial number directly, or iterate over them using Object.keys(data)).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.