OK.
We've now got to Konst a log of what we are returning from the math statement.
You'll notice that it is now just a simple object with an email property a survey ID and a choice of
this case.
Yes.
Now even though it looks like we've got a simple array here just a simple array with one object.
Remember that it is entirely possible that at some point in the future we might have records inside
that array that eventually get reduced down to just undefined.
So if we saw any event in that array of events with a event type of bounce after we do this initial
processing step that event would be replaced with just undefined.
So inside of our console log over here I would expect to see something like one event Khama undefined.
So we now need to make sure that we go through this array right here and remove any records that are
listed as undefined.
And when I say records maybe a better word here would be.
Or remove the elements that are undefined.
So we need to iterate through our list of events now and remove any undefined elements inside of it.
That's take care of that.
We can actually make use of a nother function that is inside of the load library.
This function is called compact.
The compact function takes an array.
It goes through all the elements of the array and removes any elements that are undefined.
So we create a new variable called compacted.
And what that actually it will call it just compact events is going to be underscored Daut compact events
like so Snel compact events will return only event objects and there will be no elements in there that
are undefined.
So we're now essentially at this step right here.
So the last thing we have to do is look through the array and remove any duplicate records.
Now fortunately load ash has us here as well we don't have to put down a bunch of logic to only find
actually unique records.
We can use another helper from the loadout library.
So underneath the compact events like say Konst unique events and still be Daut unique by will pass
in our compact events and then say go through this list of elements right here and look at every element.
Look at the email property and look at the survey id property and if there are any duplicates in there
just remove them.
So this line right here is going to make sure that we never have any records inside the array with the
duplicate e-mail or survey ID.
Now there is one slight weakness here R-S.C..
There is not a weakness.
I said that exact opposite.
This will make sure that it compares both the email and the survey ID.
So if one user with a given email votes on two different survey IDs it will say that's totally fine.
Like a single user can vote on multiple different surveys.
They just can't have multiple votes on the same e-mail on the same server.
So now at the very last step here let's now log out what we have a say cancel log unique events like
so and now to test our unique by check right here.
Let's go and click on our e-mail links a couple more times but this time we're going to click it a few
times and make sure that when we eventually get that console log we still only see one event some back
to my email again and I'm going to click on this thing like one two three four five times so you could
see all these tabs pop up here because I clicked on it five times.
So now when we get our consul log I'm still going to expect to see only one event.
So flip on over to my journal.
I think that yes we actually got lucky here.
I got my console log right away.
So this is with all those duplicates here.
Again you might want to pause a video at this point and wait for your console to pop up because remember
again it always takes time for this stuff to open.
So it looks like that uniqueness check worked out just fine right here.
You also know that by the way that we sometimes get these duplicates on here we get these duplicates
any time that you send grid API things that the web hook failed.
So right now if you look at our request handler we never actually respond successfully to the Web.
And so at present said Grace thinking that our requests are failing and so it keeps on repeating the
request trying to ping our server again so we can get those duplicate console logs here to just go away
if we respond to the request.
So we will do that at the very bottom here just to tell said grid hey everything is going OK.
OK.
So I think that the logic that we've added in here so far is working out just fine.
And we are successfully paring down our list of you.
However the code that we have can actually be condensed down a little bit more than what we have.
So I want to take a quick break.
We're going to come back in the next section and we're going to talk about how we can use load ash to
even further condense the logic that we have in here.
So take a quick break and then come back and fix this stuff up even more.
I'll see you in just a minute.