DEV Community


Discussion on: Easily parse an excel spreadsheet into JSON

caiangums profile image
Ilê Caian • Edited

Awesome post @nena !

Just some notes:
1 - If you use the syntax highlight at your code block it makes it more readable! For JavaScript, you just need to place javascript after your opening 'triple `'

2 - You should avoid using == and prefer ===. I wrote an article here about it 😄 :

3 - You could test just for rowVal, because the empty string counts as false on boolean checks

if (!rowVal) {
// code

Another point is that if you know that rowVal will always be a String, you could check for empty string instead:

if (rowVal.length > 0) {
// code

4 - For you JS code, instead of using multiple else if's you could use switch-case:

// switch-case
const COLUMN = {
ID: 1,
NAME: 2,
IMG: 3,
// and so on...
switch(colNum) {
case COLUMN.ID: = rowVal;
case COLUMN.NAME: = rowVal;
currentRow.img = rowVal;

5 - For this specific case, you could use Array.reduce() instead of Array.forEach(). The Array.reduce() is always a good option to be used when you want to convert a List into another type of object (String, Number, Object...)

Edit: Apparently, the "code block" at responses doesn't work properly... 😅

nena profile image
Nena Author

Wow, thanks for your great feedback!

And a special thanks for telling me how to highlight the code block. This bugged me as well but since it was my first post I didn't know how to highlight it yet. So thanks for enlightening me, I just edited it! :)

Regarding 4: I used to love switch-case but when I started coding in JavaScript I often heard that it's not as performant as if-else unless you have a looooot of elses. Do you know more about that?

As for the rest: Thanks for pointing it out, it's mostly stuff I definitely know but most of the times just forget about or are too lazy to be consistent with. 😅