What’s the difference between “{}” and “[]” while declaring a JavaScript array?

Nobody seems to be explaining the difference between an array and an object. [] is declaring an array. {} is declaring an object. An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in … Read more

Mongoose/MongoDB result fields appear undefined in Javascript

Solution You can call the toObject method in order to access the fields. For example: var itemObject = item.toObject(); console.log(itemObject.title); // “foo” Why As you point out that the real fields are stored in the _doc field of the document. But why console.log(item) => { title: “foo”, content: “bar” }? From the source code of … Read more

Comparing two arrays of objects, and exclude the elements who match values into new array in JS

well, this using lodash or vanilla javascript it depends on the situation. but for just return the array that contains the differences can be achieved by the following, offcourse it was taken from @1983 var result = result1.filter(function (o1) { return !result2.some(function (o2) { return o1.id === o2.id; // return the ones with equal id … Read more

Length of a JavaScript associative array

No, there is no built-in property that tells you how many properties the object has (which is what you’re looking for). The closest I can think of are two ES5 and higher features, Object.keys (spec | MDN) and Object.getOwnPropertyNames (spec | MDN). For instance, you could use Object.keys like this: console.log(Object.keys(quesArr).length); // “3” Object.keys returns … Read more

Create an empty object in JavaScript with {} or new Object()?

Objects There is no benefit to using new Object(); – whereas {}; can make your code more compact, and more readable. For defining empty objects they’re technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline – like so: var myObject = { title: ‘Frog’, … Read more

What is the difference between native objects and host objects?

Both terms are defined in the ECMAScript specification: native object object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment. NOTE Standard native objects are defined in this specification. Some native objects are built-in; others may be constructed during the course of execution of an ECMAScript … Read more