1 00:00:00,590 --> 00:00:01,040 OK. 2 00:00:01,080 --> 00:00:04,500 We've now got to Konst a log of what we are returning from the math statement. 3 00:00:04,500 --> 00:00:10,380 You'll notice that it is now just a simple object with an email property a survey ID and a choice of 4 00:00:10,380 --> 00:00:10,970 this case. 5 00:00:10,980 --> 00:00:12,470 Yes. 6 00:00:12,540 --> 00:00:18,000 Now even though it looks like we've got a simple array here just a simple array with one object. 7 00:00:18,000 --> 00:00:23,610 Remember that it is entirely possible that at some point in the future we might have records inside 8 00:00:23,610 --> 00:00:27,670 that array that eventually get reduced down to just undefined. 9 00:00:27,690 --> 00:00:34,110 So if we saw any event in that array of events with a event type of bounce after we do this initial 10 00:00:34,110 --> 00:00:38,660 processing step that event would be replaced with just undefined. 11 00:00:38,910 --> 00:00:45,980 So inside of our console log over here I would expect to see something like one event Khama undefined. 12 00:00:46,260 --> 00:00:51,600 So we now need to make sure that we go through this array right here and remove any records that are 13 00:00:51,600 --> 00:00:53,270 listed as undefined. 14 00:00:53,310 --> 00:00:55,800 And when I say records maybe a better word here would be. 15 00:00:55,860 --> 00:00:59,590 Or remove the elements that are undefined. 16 00:00:59,790 --> 00:01:06,180 So we need to iterate through our list of events now and remove any undefined elements inside of it. 17 00:01:06,350 --> 00:01:07,320 That's take care of that. 18 00:01:07,320 --> 00:01:13,530 We can actually make use of a nother function that is inside of the load library. 19 00:01:13,560 --> 00:01:15,810 This function is called compact. 20 00:01:15,810 --> 00:01:17,990 The compact function takes an array. 21 00:01:18,090 --> 00:01:23,420 It goes through all the elements of the array and removes any elements that are undefined. 22 00:01:23,910 --> 00:01:28,540 So we create a new variable called compacted. 23 00:01:28,550 --> 00:01:37,020 And what that actually it will call it just compact events is going to be underscored Daut compact events 24 00:01:37,020 --> 00:01:43,830 like so Snel compact events will return only event objects and there will be no elements in there that 25 00:01:43,830 --> 00:01:45,580 are undefined. 26 00:01:45,660 --> 00:01:48,980 So we're now essentially at this step right here. 27 00:01:49,020 --> 00:01:53,850 So the last thing we have to do is look through the array and remove any duplicate records. 28 00:01:53,850 --> 00:01:58,710 Now fortunately load ash has us here as well we don't have to put down a bunch of logic to only find 29 00:01:58,770 --> 00:02:00,060 actually unique records. 30 00:02:00,150 --> 00:02:02,860 We can use another helper from the loadout library. 31 00:02:03,390 --> 00:02:13,710 So underneath the compact events like say Konst unique events and still be Daut unique by will pass 32 00:02:13,710 --> 00:02:22,590 in our compact events and then say go through this list of elements right here and look at every element. 33 00:02:22,590 --> 00:02:29,490 Look at the email property and look at the survey id property and if there are any duplicates in there 34 00:02:29,700 --> 00:02:30,950 just remove them. 35 00:02:30,990 --> 00:02:36,300 So this line right here is going to make sure that we never have any records inside the array with the 36 00:02:36,300 --> 00:02:39,800 duplicate e-mail or survey ID. 37 00:02:39,810 --> 00:02:42,660 Now there is one slight weakness here R-S.C.. 38 00:02:42,840 --> 00:02:43,870 There is not a weakness. 39 00:02:43,950 --> 00:02:45,870 I said that exact opposite. 40 00:02:45,870 --> 00:02:50,090 This will make sure that it compares both the email and the survey ID. 41 00:02:50,220 --> 00:02:56,600 So if one user with a given email votes on two different survey IDs it will say that's totally fine. 42 00:02:56,670 --> 00:02:59,690 Like a single user can vote on multiple different surveys. 43 00:02:59,850 --> 00:03:05,510 They just can't have multiple votes on the same e-mail on the same server. 44 00:03:06,180 --> 00:03:12,870 So now at the very last step here let's now log out what we have a say cancel log unique events like 45 00:03:12,880 --> 00:03:19,590 so and now to test our unique by check right here. 46 00:03:19,620 --> 00:03:24,840 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 47 00:03:24,840 --> 00:03:30,510 times and make sure that when we eventually get that console log we still only see one event some back 48 00:03:30,510 --> 00:03:37,770 to my email again and I'm going to click on this thing like one two three four five times so you could 49 00:03:37,770 --> 00:03:41,190 see all these tabs pop up here because I clicked on it five times. 50 00:03:41,190 --> 00:03:46,190 So now when we get our consul log I'm still going to expect to see only one event. 51 00:03:46,470 --> 00:03:50,340 So flip on over to my journal. 52 00:03:50,490 --> 00:03:53,350 I think that yes we actually got lucky here. 53 00:03:53,520 --> 00:03:55,240 I got my console log right away. 54 00:03:55,380 --> 00:03:57,590 So this is with all those duplicates here. 55 00:03:57,630 --> 00:04:01,890 Again you might want to pause a video at this point and wait for your console to pop up because remember 56 00:04:01,890 --> 00:04:04,310 again it always takes time for this stuff to open. 57 00:04:04,440 --> 00:04:08,010 So it looks like that uniqueness check worked out just fine right here. 58 00:04:08,010 --> 00:04:12,840 You also know that by the way that we sometimes get these duplicates on here we get these duplicates 59 00:04:12,870 --> 00:04:18,060 any time that you send grid API things that the web hook failed. 60 00:04:18,060 --> 00:04:24,660 So right now if you look at our request handler we never actually respond successfully to the Web. 61 00:04:24,780 --> 00:04:29,760 And so at present said Grace thinking that our requests are failing and so it keeps on repeating the 62 00:04:29,760 --> 00:04:35,430 request trying to ping our server again so we can get those duplicate console logs here to just go away 63 00:04:35,430 --> 00:04:37,200 if we respond to the request. 64 00:04:37,200 --> 00:04:43,340 So we will do that at the very bottom here just to tell said grid hey everything is going OK. 65 00:04:43,590 --> 00:04:44,340 OK. 66 00:04:44,400 --> 00:04:48,840 So I think that the logic that we've added in here so far is working out just fine. 67 00:04:48,870 --> 00:04:52,980 And we are successfully paring down our list of you. 68 00:04:53,370 --> 00:05:00,000 However the code that we have can actually be condensed down a little bit more than what we have. 69 00:05:00,000 --> 00:05:01,310 So I want to take a quick break. 70 00:05:01,320 --> 00:05:05,790 We're going to come back in the next section and we're going to talk about how we can use load ash to 71 00:05:05,790 --> 00:05:08,400 even further condense the logic that we have in here. 72 00:05:08,670 --> 00:05:12,300 So take a quick break and then come back and fix this stuff up even more. 73 00:05:12,300 --> 00:05:14,010 I'll see you in just a minute.