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

How to loop through key/value object in Javascript? [duplicate]

Beware of properties inherited from the object’s prototype (which could happen if you’re including any libraries on your page, such as older versions of Prototype). You can check for this by using the object’s hasOwnProperty() method. This is generally a good idea when using for…in loops: var user = {}; function setUsers(data) { for (var … Read more

for each loop in Objective-C for accessing NSMutable dictionary

for (NSString* key in xyz) { id value = xyz[key]; // do stuff } This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the … Read more

How to search if dictionary value contains certain string with Python

You can do it like this: #Just an example how the dictionary may look like myDict = {‘age’: [’12’], ‘address’: [’34 Main Street, 212 First Avenue’], ‘firstName’: [‘Alan’, ‘Mary-Ann’], ‘lastName’: [‘Stone’, ‘Lee’]} def search(values, searchFor): for k in values: for v in values[k]: if searchFor in v: return k return None #Checking if string ‘Mary’ … Read more

How do you create a dictionary in Java? [closed]

You’ll want a Map<String, String>. Classes that implement the Map interface include (but are not limited to): HashMap LinkedHashMap Hashtable Each is designed/optimized for certain situations (go to their respective docs for more info). HashMap is probably the most common; the go-to default. For example (using a HashMap): Map<String, String> map = new HashMap<String, String>(); … Read more

How to create a dictionary and add key value pairs dynamically in JavaScript

Use: var dict = []; // Create an empty array dict.push({ key: “keyName”, value: “the value” }); // Repeat this last part as needed to add more key/value pairs Basically, you’re creating an object literal with two properties (called key and value) and inserting it (using push()) into the array. This does not create a … Read more

How to create dictionary and add key value pairs dynamically in Javascript

var dict = []; // create an empty array dict.push({ key: “keyName”, value: “the value” }); // repeat this last part as needed to add more key/value pairs Basically, you’re creating an object literal with 2 properties (called key and value) and inserting it (using push()) into the array. Edit: So almost 5 years later, … Read more

Java – How to create new Entry (key, value)

There’s public static class AbstractMap.SimpleEntry<K,V>. Don’t let the Abstract part of the name mislead you: it is in fact NOT an abstract class (but its top-level AbstractMap is). The fact that it’s a static nested class means that you DON’T need an enclosing AbstractMap instance to instantiate it, so something like this compiles fine: Map.Entry<String,Integer> … Read more