Filter object properties by key in ES6

If you have a list of allowed values, you can easily retain them in an object using: const raw = { item1: { key: ‘sdfd’, value:’sdfd’ }, item2: { key: ‘sdfd’, value:’sdfd’ }, item3: { key: ‘sdfd’, value:’sdfd’ } }; const allowed = [‘item1’, ‘item3’]; const filtered = Object.keys(raw) .filter(key => allowed.includes(key)) .reduce((obj, key) => … Read more

When should I use arrow functions in ECMAScript 6?

A while ago our team migrated all its code (a mid-sized AngularJS app) to JavaScript compiled using Traceur Babel. I’m now using the following rule of thumb for functions in ES6 and beyond: Use function in the global scope and for Object.prototype properties. Use class for object constructors. Use => everywhere else. Why use arrow … Read more

What’s the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript?

What It Is This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {…}. But they have some important differences. For example, … Read more

Node.js – SyntaxError: Unexpected token import

Node 13+ Since Node 13, you can use either the .mjs extension, or set {“type”: “module”} in your package.json. You don’t need to use the –experimental-modules flag. Modules is now marked as stable in node.js Node 12 Since Node 12, you can use either the .mjs extension, or set “type”: “module” in your package.json. And … Read more

Private properties in JavaScript ES6 classes

Update: See others answer, this is outdated. Short answer, no, there is no native support for private properties with ES6 classes. But you could mimic that behaviour by not attaching the new properties to the object, but keeping them inside a class constructor, and use getters and setters to reach the hidden properties. Note that … Read more

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