1
00:00:00,270 --> 00:00:05,610
Back in this lesson, we're going to clean up the contents of item as we're looping through it, so

2
00:00:05,610 --> 00:00:08,380
let's create a new function and we'll just call this one clean.

3
00:00:08,640 --> 00:00:15,360
So what this will do is this is actually going to clean up our court and we'll pass that in to a clean

4
00:00:15,360 --> 00:00:15,900
function.

5
00:00:17,270 --> 00:00:24,560
And we'll return back an updated and a cleaned out version, so instead of doing our join, we're going

6
00:00:24,560 --> 00:00:28,750
to update it as clean and we can still include the new line breaks.

7
00:00:28,750 --> 00:00:29,420
So that's fine.

8
00:00:29,420 --> 00:00:30,470
We can keep that in there.

9
00:00:30,740 --> 00:00:34,580
So now we've got a new function that's going to take in the content of the item.

10
00:00:34,790 --> 00:00:38,540
So we have it still within us, an array format.

11
00:00:38,690 --> 00:00:42,240
So that means that we can loop through each one of the items that are available.

12
00:00:42,650 --> 00:00:44,080
So let's do that as well.

13
00:00:44,690 --> 00:00:47,240
And then as we loop through each one of the items.

14
00:00:47,240 --> 00:00:48,860
So these are going to be for the values.

15
00:00:49,940 --> 00:00:51,170
So actually, I'm going to update this.

16
00:00:51,170 --> 00:00:57,980
I'm going to say roll and this is going to loop through each one of the rows and we can just call it

17
00:00:57,980 --> 00:01:01,200
cell and for now will console outsell.

18
00:01:01,430 --> 00:01:06,560
So refresh, try that out and we'll see that we're getting all of the contents of each one of the cells.

19
00:01:06,770 --> 00:01:12,380
We do have a problem here in number 58, and that's because we're not getting those values within Holder

20
00:01:12,380 --> 00:01:14,850
anymore as we're cleaning those items out.

21
00:01:15,020 --> 00:01:19,520
Holder is coming back as undefined, undefined, undefined, because we're not returning back any of

22
00:01:19,520 --> 00:01:20,120
the values.

23
00:01:20,400 --> 00:01:26,420
So let's make sure that we are returning back those values of the cells and constructing each one of

24
00:01:26,450 --> 00:01:29,600
those cell values as we're building it.

25
00:01:30,200 --> 00:01:35,810
So taking that values so and set that value to be blank.

26
00:01:35,810 --> 00:01:39,470
And then we're also going to return back whatever the value of responses.

27
00:01:39,920 --> 00:01:42,200
So we actually get something back coming in there.

28
00:01:43,470 --> 00:01:50,940
So as we loop through the cells, first, let's check to see if the value of the cell is null and if

29
00:01:50,940 --> 00:01:52,450
it is, we'll make an update on that.

30
00:01:52,740 --> 00:01:56,660
So checking to see if cell is equal to null.

31
00:01:57,150 --> 00:02:00,740
And if it is, then we're going to just keep it as blank.

32
00:02:01,500 --> 00:02:08,760
Otherwise, we take the value of cell and apply to string so that we make sure that each one of those

33
00:02:08,760 --> 00:02:10,590
values of cell is a string.

34
00:02:12,370 --> 00:02:17,950
Another thing that we want to do is we want to wrap our cells in.

35
00:02:19,110 --> 00:02:26,700
In quotes, if it contains certain values such as a comma or if it's got a line break or if it's got

36
00:02:26,700 --> 00:02:32,790
a double quote, so if any of these match and one of the tools I like to use in order to build regular

37
00:02:32,790 --> 00:02:35,280
expressions is regex dot com.

38
00:02:35,520 --> 00:02:41,220
So it's a great way to try out your different regular expressions before they use them within your JavaScript.

39
00:02:41,730 --> 00:02:48,720
So going back into our code, let's take a look and we're going to see what the value of cell is by

40
00:02:48,720 --> 00:02:50,270
doing a search on cell.

41
00:02:50,940 --> 00:02:55,260
So applying a condition and we're taking the value of cell.

42
00:02:56,040 --> 00:02:58,710
We're going to search that value of cell.

43
00:02:58,710 --> 00:03:01,920
And this is where we're applying our regular expression that we just built.

44
00:03:03,510 --> 00:03:08,730
I wanted to start that with a backslash because it doesn't copy that in for some reason and.

45
00:03:10,290 --> 00:03:11,880
So this is the regular expression.

46
00:03:12,720 --> 00:03:18,750
And if we find any occurrences of that, so if it's greater than zero, then we're going to take the

47
00:03:18,750 --> 00:03:22,720
value of cell and we're going to equal that to be.

48
00:03:23,010 --> 00:03:28,770
So that's a single quote, double quote, single quote and sell.

49
00:03:29,430 --> 00:03:35,310
So basically what we're doing here is we're double coding around any one of these items.

50
00:03:35,520 --> 00:03:42,260
So that will prevent any kind of problems we might have with the cell as we're building.

51
00:03:42,810 --> 00:03:49,380
And then once we've cleaned up the cells as we iterate through it, we're going to add to that response

52
00:03:49,380 --> 00:03:52,650
value, whatever the value of cell is going to be.

53
00:03:52,920 --> 00:03:55,520
So now we can try that and we can refresh.

54
00:03:55,530 --> 00:04:00,090
And when we go download, you see that now we've got more of a cleaned up content format.

55
00:04:00,660 --> 00:04:05,330
You can see something is still not right because we didn't add in the commas properly.

56
00:04:05,580 --> 00:04:08,880
So we need to make sure that those get added in as well to our result.

57
00:04:09,240 --> 00:04:13,590
So we're going to make sure that we're adding our commas back into our object.

58
00:04:14,310 --> 00:04:20,280
And as we're looping through it, we can also get an index value so we can console logout that index

59
00:04:20,280 --> 00:04:20,640
value.

60
00:04:21,100 --> 00:04:28,890
You can see how we can use this to check to see which value we're on if we have anything larger than

61
00:04:28,890 --> 00:04:30,070
index zero.

62
00:04:30,090 --> 00:04:35,250
So if our index value is greater than zero, then we're going to add in a comma.

63
00:04:35,280 --> 00:04:36,690
So we're going to take Repp.

64
00:04:37,560 --> 00:04:44,650
And add a comma into it, so this will give us a comma in everything was returning those values.

65
00:04:44,880 --> 00:04:47,730
Now we've got it more of a comma separated format.

66
00:04:47,820 --> 00:04:55,470
And if we open that new file up, we see that now that comma has been handled properly within our column

67
00:04:55,470 --> 00:04:57,900
and we're still outputting the data as expected.

68
00:04:57,990 --> 00:05:04,140
So I know that was quite a bit of a fix for the content that's being output to go ahead and apply that

69
00:05:04,140 --> 00:05:09,030
into your own structure in order to better prepare for the CSV file.
