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

Does this way of defining JS objects have any purpose?

It’s called the module pattern http://toddmotto.com/mastering-the-module-pattern/ The main reason is for you to create truly private methods and variables. In your case, it’s not meaningful because it’s not hiding any implementation details. Here’s an example where it makes sense to use the module pattern. var MyNameSpace = {}; (function(ns){ // The value variable is hidden … Read more

Create an object from an array of keys and an array of values

var keys = [‘foo’, ‘bar’, ‘baz’]; var values = [11, 22, 33] var result = {}; keys.forEach((key, i) => result[key] = values[i]); console.log(result); Alternatively, you can use Object.assign result = Object.assign(…keys.map((k, i) => ({[k]: values[i]}))) or the object spread syntax (ES2018): result = keys.reduce((o, k, i) => ({…o, [k]: values[i]}), {}) or Object.fromEntries (ES2019): Object.fromEntries(keys.map((_, … Read more

Javascript ES6 spread operator on undefined [duplicate]

This behavior is useful for doing something like optional spreading: function foo(options) { const bar = { baz: 1, …(options && options.bar) // options and bar can be undefined } } And it gets even better with optional chaining, which is in Stage 4 now (and already available in TypeScript 3.7+): function foo(options) { const … Read more

Encoding Javascript Object to Json string

Unless the variable k is defined, that’s probably what’s causing your trouble. Something like this will do what you want: var new_tweets = { }; new_tweets.k = { }; new_tweets.k.tweet_id = 98745521; new_tweets.k.user_id = 54875; new_tweets.k.data = { }; new_tweets.k.data.in_reply_to_screen_name=”other_user”; new_tweets.k.data.text=”tweet text”; // Will create the JSON string you’re looking for. var json = JSON.stringify(new_tweets); … Read more

Is there a limit on length of the key (string) in JS object?

I have researched this a bit. MDN is silent on the issue, and so is the spec (ES5, ES6). They only state that the property accessor must be a string, without any qualifications – in other words, there is no limit as far as the spec is concerned. That’s hardly surprising. How browsers handle it, … 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

Why can I add named properties to an array as if it were an object?

Virtually everything in javascript is an object, so you can “abuse” an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays are for numerically indexed data – for non-numeric keys, use an Object. Here’s a more concrete example why non-numeric keys don’t “fit” an Array: var myArray = Array(); … Read more

Sorting arrays numerically by object property value

Use Array.prototype.sort(), eg myArray.sort((a, b) => a.distance – b.distance) The sort() method accepts a comparator function. This function accepts two arguments (both presumably of the same type) and it’s job is to determine which of the two comes first. It does this by returning an integer Negative (less-than zero): The first argument comes first Positive … Read more