espresso_coffee espresso_coffee - 1 year ago 63
Javascript Question

Comparing array value in javascript?

I was working on my project where I had to compare some ID's and check if they are the same or not. First time I saw something like this

if (found[myRes[i].eventId] === 1) {

So I have array myRes that looks like this:

myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Mike, Allan"});

myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Gates, Bill"});

myRes.push({'eventId':19,'eventName':"Spring 2016",'rCustomer':"John, Bill"});

myRes.push({'eventId':19,'eventName':"Spring 2016",'rCustomer':"Adams, Ron"});

myRes.push({'eventId':31,'eventName':"May Test 1",'rCustomer':"Steve, Marie"});

myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Ariel, Bill"});

myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Ron, Nill"});

myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Kim, Alen"});

myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Will, Huges"});

myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Seth, Peak"});

I have solution for my problem but I do not understand completely how it works. Here is the code that I use:

var found = [];

for (var i = 0; i < myRes.length; i++) {
if (found[myRes[i].eventId] === 1) {
$('#row_' + myRes[i].eventId).append('<tr><td>'+myRes[i].some element+'</td></tr></tbody></table>');
} else {
found[myRes[i].eventId] = 1;
$('.myReservation').append('<table><tr></tr><thead><tr><th >'+myRes[i].eventName+'</th></tr></thead><tbody id="row_' + myRes[i].eventId + '"><tr><td>'+myRes[i].someelement+'</td></tr>');

Here is the method that I used and did not work, this method outputted same event Name twice.

var eventId;

for(var i=0; i< myRes.length; i++){
if(eventId === myRes[i].eventId){
$('#row_' + myRes[i].eventId).append('<td>'+myRes[i].rCustomer+'</td>');
eventId = myRes[i].eventId;
$('.myReservation').append('<tr id="row_'+myRes[i].eventId+'"><td><b>Event: '+myRes[i].eventName+'</b></td><td>'+myRes[i].rCustomer+'</td></tr>');

Output from code above:

Event: Test 1 Mike, Allan Gates, Bill
Event: Spring 2016 John, Bill Adams, Ron
Event: May Test 1 Steve, Marie
Event: Test 1 Ariel, Bill
Event: Fall 2016 Ron, Nill Kim, Alen Will, Huges Seth, Peak

  1. My question is what actually happens in my if statement?

  2. Does if statement basically looks for specific id that is assigned
    in found array and if does not exist in found array then left side
    of my if statement is 1 that will execute if statement? Or something
    else is behind that.

  3. Also in else statement new value is assigned to found array and set
    to 1. How that actually looks in found array then? Is that specific
    ID set to 1?

  4. If anyone can explain this to me I would appreciate. I tried to
    output found array to the screen but still that did not gave me
    enough to understand how this works. Thanks in advance.

Answer Source

Your code only compares with the most recent eventId, since there's just a single variable. This would work OK if you had myRes sorted by eventId, since it would group all the reservations for same event together and just show the event name once at the beginning of the group. But since the events are scattered in the array, it shows the event name whenever it changes, so you get it multiple times.

The way the first code works is that it uses the event ID as the index in the found array. Initially the array is empty, so found[anything] will be undefined. The first time it encounters an event ID in the loop, found[myRes[i].eventId] will be undefined, which is treated as false by if. That makes it go to the else block, which displays the event name and sets found[myRes[i].eventId] = 1. The next time it encounters that same event ID in the loop, the test succeeds, so it performs the code in the if block, which just displays the customer data.

For example, myRes[0].eventId is 42. So the first iteration of the loop does

if (found[42] === 1)

Since this fails, it does:

found[42] = 1;

in the else block. In later iterations of the loop, when myRes[i].eventId is again 42 (such as the 2nd and 6th reservations), the test

if (found[42] === 1)

will succeed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download