Back in this lesson, we're going to clean up the contents of item as we're looping through it, so
let's create a new function and we'll just call this one clean.
So what this will do is this is actually going to clean up our court and we'll pass that in to a clean
function.
And we'll return back an updated and a cleaned out version, so instead of doing our join, we're going
to update it as clean and we can still include the new line breaks.
So that's fine.
We can keep that in there.
So now we've got a new function that's going to take in the content of the item.
So we have it still within us, an array format.
So that means that we can loop through each one of the items that are available.
So let's do that as well.
And then as we loop through each one of the items.
So these are going to be for the values.
So actually, I'm going to update this.
I'm going to say roll and this is going to loop through each one of the rows and we can just call it
cell and for now will console outsell.
So refresh, try that out and we'll see that we're getting all of the contents of each one of the cells.
We do have a problem here in number 58, and that's because we're not getting those values within Holder
anymore as we're cleaning those items out.
Holder is coming back as undefined, undefined, undefined, because we're not returning back any of
the values.
So let's make sure that we are returning back those values of the cells and constructing each one of
those cell values as we're building it.
So taking that values so and set that value to be blank.
And then we're also going to return back whatever the value of responses.
So we actually get something back coming in there.
So as we loop through the cells, first, let's check to see if the value of the cell is null and if
it is, we'll make an update on that.
So checking to see if cell is equal to null.
And if it is, then we're going to just keep it as blank.
Otherwise, we take the value of cell and apply to string so that we make sure that each one of those
values of cell is a string.
Another thing that we want to do is we want to wrap our cells in.
In quotes, if it contains certain values such as a comma or if it's got a line break or if it's got
a double quote, so if any of these match and one of the tools I like to use in order to build regular
expressions is regex dot com.
So it's a great way to try out your different regular expressions before they use them within your JavaScript.
So going back into our code, let's take a look and we're going to see what the value of cell is by
doing a search on cell.
So applying a condition and we're taking the value of cell.
We're going to search that value of cell.
And this is where we're applying our regular expression that we just built.
I wanted to start that with a backslash because it doesn't copy that in for some reason and.
So this is the regular expression.
And if we find any occurrences of that, so if it's greater than zero, then we're going to take the
value of cell and we're going to equal that to be.
So that's a single quote, double quote, single quote and sell.
So basically what we're doing here is we're double coding around any one of these items.
So that will prevent any kind of problems we might have with the cell as we're building.
And then once we've cleaned up the cells as we iterate through it, we're going to add to that response
value, whatever the value of cell is going to be.
So now we can try that and we can refresh.
And when we go download, you see that now we've got more of a cleaned up content format.
You can see something is still not right because we didn't add in the commas properly.
So we need to make sure that those get added in as well to our result.
So we're going to make sure that we're adding our commas back into our object.
And as we're looping through it, we can also get an index value so we can console logout that index
value.
You can see how we can use this to check to see which value we're on if we have anything larger than
index zero.
So if our index value is greater than zero, then we're going to add in a comma.
So we're going to take Repp.
And add a comma into it, so this will give us a comma in everything was returning those values.
Now we've got it more of a comma separated format.
And if we open that new file up, we see that now that comma has been handled properly within our column
and we're still outputting the data as expected.
So I know that was quite a bit of a fix for the content that's being output to go ahead and apply that
into your own structure in order to better prepare for the CSV file.