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

How to do a deep comparison between 2 objects with lodash?

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

How to get all properties values of a JavaScript Object (without knowing the keys)?

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

How to return value from an asynchronous callback function? [duplicate]

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

How to iterate over a JavaScript object?

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

How can I display a JavaScript object?

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)