Getting the first index of an object
Just for fun this works in JS 1.8.5 var obj = {a: 1, b: 2, c: 3}; Object.keys(obj)[0]; // “a” This matches the same order that you would see doing for (o in obj) { … }
Just for fun this works in JS 1.8.5 var obj = {a: 1, b: 2, c: 3}; Object.keys(obj)[0]; // “a” This matches the same order that you would see doing for (o in obj) { … }
You don’t need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the data argument should be quoted. First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it easier to read … Read more
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
An easy and elegant solution is to use _.isEqual, which performs a deep comparison: var a = {}; var b = {}; a.prop1 = 2; a.prop2 = { prop3: 2 }; b.prop1 = 2; b.prop2 = { prop3: 3 }; console.log(_.isEqual(a, b)); // returns false if different <script src=”https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js”></script> However, this solution doesn’t show which … Read more
Depending on which browsers you have to support, this can be done in a number of ways. The overwhelming majority of browsers in the wild support ECMAScript 5 (ES5), but be warned that many of the examples below use Object.keys, which is not available in IE < 9. See the compatibility table. ECMAScript 3+ If … Read more
This is impossible as you cannot return from an asynchronous call inside a synchronous method. In this case you need to pass a callback to foo that will receive the return value function foo(address, fn){ geocoder.geocode( { ‘address’: address}, function(results, status) { fn(results[0].geometry.location); }); } foo(“address”, function(location){ alert(location); // this is where you get the … Read more
For iterating on keys of Arrays, Strings, or Objects, use for .. in : for (let key in yourobject) { console.log(key, yourobject[key]); } With ES6, if you need both keys and values simultaneously, do for (let [key, value] of Object.entries(yourobject)) { console.log(key, value); } To avoid logging inherited properties, check with hasOwnProperty : for (let … Read more
__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new: ( new Foo ).__proto__ === Foo.prototype ( new Foo ).prototype === undefined
Here is a shorter way of achieving it: let result = objArray.map(a => a.foo); OR let result = objArray.map(({ foo }) => foo) You can also check Array.prototype.map().
Use native JSON.stringify method. Works with nested objects and all major browsers support this method. str = JSON.stringify(obj); str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output. console.log(str); // Logs output to dev tools console. alert(str); // Displays output using window.alert() Link to Mozilla API Reference and other examples. obj = JSON.parse(str); // Reverses … Read more