Tuesday, August 13, 2013

my foray into javascript

I was an Oracle PeopleSoft developer for over 5 years and just over a year ago decided to write custom software using non proprietary tools (JAVA, Ruby, e.t.c) with .Net being an exception. I've been doing back end development in Java and have had very little contact with Javascript. 

On my present project, we ran into a bug when refactoring some JavaScript. This bug involved comparing two objects using a predicate being passed into an underscoreJs function _.some.

For those not familiar with underscoreJs's _.some, it takes a collection and a predicate, and returns a boolean if a search based on the predicate matches or not as seen below:


When you try this with an object, it fails. This made us scratch our collective heads for quite a while. running this fails:


The solution was to use JSON.stringify(arg) to convert the object to string literal. This would not work if the fields in the object are not in the exact order as shown below. Preferably you should compare on the object fields. But for what we wanted, that was not an option.

No comments:

Post a Comment