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

what is Object Cloning in php?

Object cloning is the act of making a copy of an object. As Cody pointed out, cloning in PHP is done by making a shallow copy of the object. This means that internal objects of the cloned object will not be cloned, unless you explicitly instruct the object to clone these internal objects too, by … Read more

Reliably convert any object to String and then back again

Yes, it is called serialization! String serializedObject = “”; // serialize the object try { ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream so = new ObjectOutputStream(bo); so.writeObject(myObject); so.flush(); serializedObject = bo.toString(); } catch (Exception e) { System.out.println(e); } // deserialize the object try { byte b[] = serializedObject.getBytes(); ByteArrayInputStream bi = new ByteArrayInputStream(b); ObjectInputStream si = … Read more

Creating nested dataclass objects in Python

You can use post_init for this from dataclasses import dataclass @dataclass class One: f_one: int f_two: str @dataclass class Two: f_three: str f_four: One def __post_init__(self): self.f_four = One(**self.f_four) data = {‘f_three’: ‘three’, ‘f_four’: {‘f_one’: 1, ‘f_two’: ‘two’}} print(Two(**data)) # Two(f_three=”three”, f_four=One(f_one=1, f_two=’two’))

Node.js – Using the async lib – async.foreach with object

The final function does not get called because async.forEach requires that you call the callback function for every element. Use something like this: async.forEach(Object.keys(dataObj), function (item, callback){ console.log(item); // print the key // tell async that that particular element of the iterator is done callback(); }, function(err) { console.log(‘iterating done’); });