Array Like Objects in Javascript

The object has to have length and splice > var x = {length:2, ‘0’:’foo’, ‘1’:’bar’, splice:function(){}} > console.log(x); [‘foo’, ‘bar’] and FYI, the Object[0] as the prototype is for exactly the same reason. The browser is seeing the prototype itself as an array because: $.prototype.length == 0; $.prototype.splice == [].splice;

Javascript creating objects – multiple approaches, any differences?

7 ways to create objects in JavaScript : 1. Object constructor The simplest way to create an object is to use the Object constructor: view plainprint? var person = new Object(); person.name = “Diego”; person.getName = function(){ return this.name; }; 2. Literal notation view plainprint? var person = { person.name : “Diego”, person.getName : function(){ … Read more

Array of object deep comparison with lodash

You can make use of differenceWith() with an isEqual() comparator, and invoke isEmpty to check if they are equal or not. var isArrayEqual = function(x, y) { return _(x).differenceWith(y, _.isEqual).isEmpty(); }; var result1 = isArrayEqual( [{a:1, b:2}, {c:3, d:4}], [{b:2, a:1}, {d:4, c:3}] ); var result2 = isArrayEqual( [{a:1, b:2, c: 1}, {c:3, d:4}], [{b:2, … Read more

how to check if all object keys has false values

Updated version. Thanks @BOB for pointing out that you can use values directly: Object.values(obj).every((v) => v === false) Also, the question asked for comparison to false and most answers below return true if the object values are falsy (eg. 0, undefined, null, false), not only if they are strictly false. This is a very simple … Read more

What’s the recommended way of creating objects in NodeJS?

The most efficient way is the following: Put all the essential initialisation in the constructor (e.g. validate constructor parameters, set properties, etc). Set methods in the .prototype property of the constructor. Why? Because this prevents from re-writing each method each time you create an object. This way you recycle the same prototype for each object … Read more

Are Javascript arrays primitives? Strings? Objects?

Arrays are objects. However, unlike regular objects, arrays have certain special features. Arrays have an additional object in their prototype chain – namely Array.prototype. This object contains so-called Array methods which can be called on array instances. (List of methods is here: http://es5.github.io/#x15.4.4) Arrays have a length property (which is live, ergo, it auto-updates) (Read … Read more