简体   繁体   中英

select a column in xlsx file in nodejs

I'm not trying to do anything fancy just select a column from an xlsx file as a variable. Well what I am trying to do overall is to take a list of numbers from a column and count the occurrences. I have two columns of numbers and want to count them seperately.

I am new to JS and all that I just wanted something that I thought would be easy to start with. Here is the code I have so far.

var xlsx =  require("xlsx");

var numbersWB = xlsx.readFile('lucky_for_life_numbers.xlsx');//imports the file
var ws = numbersWB.Sheets['Drawn'];//gets the Sheet 'Drawn' for the drawn numbers

var data = xlsx.utils.sheet_to_json(ws);//converts the drawn column to something readable

console.log(data)//displays the data

And here is some sample data.

[
  { numbers: '6 - 8 - 14 - 25 - 42', lucky_numbers: 6 },
  { numbers: '1 - 12 - 17 - 28 - 44', lucky_numbers: 14 },
  { numbers: '1 - 10 - 24 - 26 - 30', lucky_numbers: 9 },
  { numbers: '5 - 23 - 26 - 29 - 36', lucky_numbers: 10 },
  { numbers: '8 - 15 - 19 - 46 - 47', lucky_numbers: 18 },
  { numbers: '2 - 6 - 24 - 32 - 39', lucky_numbers: 11 },
  { numbers: '4 - 5 - 35 - 37 - 48', lucky_numbers: 2 },
  //  ... 1319 more items
]

It's just like the power ball lottery - choose 5 numbers and a lucky number.

I have the data in an Excel spreadsheet and was looking to count the occurrences of the numbers by column, or lowest number to highest. For example:

Column 1 1:2 ,2:1, 4:1, 5:1, 6:1, 8:1  
Column 2 5:1, 6:1, 8:1......
lucky numbers 2:1, 6:1, 9:1.......

And so on

The 'numbers:' is a column and the 'lucky_numbers:' is a separate column in the spreadsheet but, this is how they are formatted as json data.

Here is a sample of the excel data

Sample of the excel data

I didn't think it was going to be so hard to get this down. I did a little something like this in python and had pandas to help with all of the fun stuff.

Any help would be appreciated. Thanks, and have a great day.

TL;DR - map is your friend.

Use map to isolate a column of the data eg

 const data = [ { numbers: '6 - 8 - 14 - 25 - 42', lucky_numbers: 6 }, { numbers: '1 - 12 - 17 - 28 - 44', lucky_numbers: 14 }, { numbers: '1 - 10 - 24 - 26 - 30', lucky_numbers: 9 }, { numbers: '5 - 23 - 26 - 29 - 36', lucky_numbers: 10 }, { numbers: '8 - 15 - 19 - 46 - 47', lucky_numbers: 18 }, { numbers: '2 - 6 - 24 - 32 - 39', lucky_numbers: 11 }, { numbers: '4 - 5 - 35 - 37 - 48', lucky_numbers: 2 }, //... 1319 more items ]; const lucky = data.map(d => d.lucky_numbers); console.log(lucky);

To split the string array of numbers use map and split . Then to transpose the array to get a one row, per column, see this answer (again using map ):

 const data = [ { numbers: '6 - 8 - 14 - 25 - 42', lucky_numbers: 6 }, { numbers: '1 - 12 - 17 - 28 - 44', lucky_numbers: 14 }, { numbers: '1 - 10 - 24 - 26 - 30', lucky_numbers: 9 }, { numbers: '5 - 23 - 26 - 29 - 36', lucky_numbers: 10 }, { numbers: '8 - 15 - 19 - 46 - 47', lucky_numbers: 18 }, { numbers: '2 - 6 - 24 - 32 - 39', lucky_numbers: 11 }, { numbers: '4 - 5 - 35 - 37 - 48', lucky_numbers: 2 }, //... 1319 more items ]; const rowNumbers = data.map(d => d.numbers).map(str => str.split(" - ")); // see https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript const colNumbers = rowNumbers[0].map((_, index) => rowNumbers.map(row => row[index])); console.log(colNumbers);

To count the frequency of each unique number in an array use Set to get unique numbers and map with filter to get occurrences eg:

 var arr = [ 6, 1, 1, 5, 8, 2, 4 ]; // first 'column' of numbers var uniques = [... new Set(arr)]; var freqs = uniques.map(n => ({[n]: arr.filter(k => k === n).length })); console.log(JSON.stringify(freqs));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM