Jump to content
iwato

Understanding the if ( A in B) { ... } Statement

Recommended Posts

ACKNOWLEDGMENT and RESULTS:  Let me begin by announcing my success in having merged Matomo and wordcount2.js into a dynamically loading word cloud that reflects visitor use of Grammar Captive's local search engines.  It is a little slow in loading, but unfortunately everything related to Matomo is slow at this point -- this, despite my recent server upgrade that has sped everything up. 

If you would like to see the result, simply click on the menu option Word Cloud under Visitor Profile in the navigation bar on the Grammar Captive mainpage.   And, now back to Javascript.  Thank you everyone for your effort.

BACKGROUND:  In an effort to distinguish between visitor desire and Grammar Captive's ability to meet this desire, I am seeking to create another word cloud that measures only visitor behavior.  The aforementioned word cloud reflects a matching of visitor desire with Grammar Captives ability to match it.  It is not a true reflection of visitor desire.  In order to achieve this latter I must count not the number of hits that visitors receive when they make a search, rather I must count the number of searches for a particular item.  In order to do this I found a function on Stack Overflow that will likely be of help, if only I could understand it.

The FUNCTION:

function checkDuplicateInObject(propertyName, inputArray) {
	var seenDuplicate = false,
	testObject = {};
	inputArray.map(function(item) {
		var itemPropertyName = item[propertyName];    
		if (itemPropertyName in testObject) {
			testObject[itemPropertyName].duplicate = true;
			item.duplicate = true;
			seenDuplicate = true;
		}
		else {
			testObject[itemPropertyName] = item;
			delete item.duplicate;
		}
	});
	return seenDuplicate;
}

MY QUANDARY:  The condition of the above function's if-statement appears to be self-defeating.  Still, the function works, for I have tested it with my own data. Specifically, the variable testObject appears to contain an undefined object. It would appear then that the condition of the if-statement would always return false.  As this is definitely not the case, I am at a loss as to how to interpret the condition.

QUESTION:  How is it that testObject can be empty and the condition still return true?

Roddy

 

 

 

 

Share this post


Link to post
Share on other sites

testObject starts off empty, but the code is adding elements to it on each iteration. Specifically on this line:

testObject[itemPropertyName] = item;

 

Share this post


Link to post
Share on other sites

So, it is false the first time around, performs the else-statement, loops through again, but on the second loop is a true expression. Is this it?

Roddy

Share this post


Link to post
Share on other sites

It will be true if the next item in the array has the same property as any previous item.  It's checking for a specific property though, not just any property.  So if you called the function like this:

checkDuplicateInObject('count', arrayOfObjects);

then that will return true if 2 or more objects in that array have a count property.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×