Cast plain object to mongoose document

Posting my own answer so this doesn’t stay open: Version 4 models (stable released on 2015-03-25) now exposes a hydrate() method. None of the fields will be marked as dirty initially, meaning a call to save() will do nothing until a field is mutated. https://github.com/LearnBoost/mongoose/blob/41ea6010c4a84716aec7a5798c7c35ef21aa294f/lib/model.js#L1639-1657 It is very important to note that this is intended … Read more

jQuery recursive iteration over objects

The .find(‘selector’) method is basically a recusive version of .children(), and will find any descendant object that matched the selector, as opposed to .children() which only finds objects in the first level of descendants. 2nd EDIT (I phrased badly the first time, and messed up the code a bit!): Ok, I don’t think this functionality … Read more

How to get a list of key values from array of JavaScript objects [duplicate]

You can take Array.map(). This method returns an array with the elements from the callback returned. It expect that all elements return something. If not set, undefined will be returned. var students = [{ name: ‘Nick’, achievements: 158, points: 14730 }, { name: ‘Jordan’, achievements: ‘175’, points: ‘16375’ }, { name: ‘Ramon’, achievements: ’55’, points: … Read more

class ClassName versus class ClassName(object) [duplicate]

In Python 2.x, when you inherit from “object” you class is a “new style” class – that was implemented back in Python 2.2 (around 2001) – The non inheriting from “object” case creates an “old style” class, that was actually maintained only for backwards compatibility. The great benefit of “new style” classes is the unification … Read more

ES5 Object.assign equivalent

In underscore.js you can use like, _.extend(firstObj, secondObj); In jQuery, you can use, $.extend({},firstObj,secondObj); In pure javascipt, you can merge n number of objects by using this function: function mergeObjects() { var resObj = {}; for(var i=0; i < arguments.length; i += 1) { var obj = arguments[i], keys = Object.keys(obj); for(var j=0; j < … Read more

Openpyxl check for empty cell

To do something when cell is not empty add: if cell.value: which in python is the same as if cell value is not None (i.e.: if not cell.value == None:) Note to avoid checking empty cells you can use worksheet.get_highest_row() and worksheet.get_highest_column() Also I found it useful (although might not be a nice solution) if … Read more

Filter array of objects by array of ids

You can simply use array.filter with indexOf to check the matching element in the next array. var arr = serviceList.filter(item => activeIds.indexOf(item.id) === -1); DEMO let activeIds = [202, 204] let serviceList = [{ “id”:201, “title”:”a” }, { “id”:202, “title”:”a” }, { “id”:203, “title”:”c” }, { “id”:204, “title”:”d” }, { “id”:205, “title”:”e” }]; let arr … Read more