JavaScript: Remove duplicates of objects sharing same property value

This function removes duplicate values from an array by returning a new one. function removeDuplicatesBy(keyFn, array) { var mySet = new Set(); return array.filter(function(x) { var key = keyFn(x), isNew = !mySet.has(key); if (isNew) mySet.add(key); return isNew; }); } var values = [{color: “red”}, {color: “blue”}, {color: “red”, number: 2}]; var withoutDuplicates = removeDuplicatesBy(x => … Read more

Ruby – getting value of hash

Convert the key from a string to a symbol, and do a lookup in the hash. hash = {:key1 => “value1”, :key2 => “value2”} k = ‘key1′ hash[k.to_sym] # or iow, hash[:key1], which will return “value1” Rails uses this class called HashWithIndifferentAccess that proves to be very useful in such cases. I know that you’ve … Read more

iPhone – How to detect if a key exists in NSUserDefaults standardUserDefaults

Do it the right way and register default values. NSDictionary *userDefaultsDefaults = @{ @”SomeKey”: @NO, @”AnotherKey”: @”FooBar”, @”NumberKey”: @0, }; [NSUserDefaults.standardUserDefaults registerDefaults:userDefaultsDefaults]; do this before you use anything from NSUserDefaults. The beginning of application:didFinishLaunchingWithOptions: is a safe place. You have to register the defaults each time the app launches. NSUserDefaults only stores values that have … Read more

Fastest, non-memory-based, multi-process key-value store for Node.js

I would suggest to have a look at LMDB (which is the most efficient engine for OpenLDAP, and used in a number of other open-source projects). LMDB is an embedded key/value store, with a Berkeley-DB or LevelDB like API, does not have to store everything in memory, and can support access from multiple processes. There … Read more

How to check if keys exists and retrieve value from Dictionary in descending priority

One option if the number of keys is small is to use chained gets: value = myDict.get(‘lastName’, myDict.get(‘firstName’, myDict.get(‘userName’))) But if you have keySet defined, this might be clearer: value = None for key in keySet: if key in myDict: value = myDict[key] break The chained gets do not short-circuit, so all keys will be … Read more

tech