How do I merge two javascript objects together in ES6+?

You will be able to do a shallow merge/extend/assign in ES6 by using Object.assign: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign Syntax: Object.assign(target, sources); where …sources represents the source object(s). Example: var obj1 = {name: ‘Daisy’, age: 30}; var obj2 = {name: ‘Casey’}; Object.assign(obj1, obj2); console.log(obj1.name === ‘Casey’ && obj1.age === 30); // true

Using _ (underscore) variable with arrow functions in ES6/Typescript

The reason why this style can be used (and possibly why it was used here) is that _ is one character shorter than (). Optional parentheses fall into the same style issue as optional curly brackets. This is a matter of taste and code style for the most part, but verbosity is favoured here because … Read more

Does Jest support ES6 import/export?

From my answer to another question, this can be simpler: The only requirement is to configure your test environment to Babel, and add the ECMAScript 6 transform plugin: Step 1: Add your test environment to .babelrc in the root of your project: { “env”: { “test”: { “plugins”: [“@babel/plugin-transform-modules-commonjs”] } } } Step 2: Install the … Read more

Using ECMAScript 6

In Chrome, most of the ES6 features are hidden behind a flag called “Experimental JavaScript features”. Visit chrome://flags/#enable-javascript-harmony, enable this flag, restart Chrome and you will get many new features. Arrow functions are not yet implemented in V8/Chrome, so this flag won’t “unlock” arrow functions. Since arrow functions are a syntax change, it is not … Read more

Create object from array

Simply const obj = {}; for (const key of yourArray) { obj[key] = whatever; } or if you prefer “functional” style: const obj = yourArray.reduce((o, key) => Object.assign(o, {[key]: whatever}), {}); using the modern object spread operator: const obj = yourArray.reduce((o, key) => ({ …o, [key]: whatever}), {}) Example: [ { id: 10, color: “red” … Read more

ECMAScript 6 features available in Node.js 0.12

Features without –harmony flag: “for-of” loop Map, Set, WeakMap, WeakSet (already specified in question) Symbol (already specified in question) Promise (already specified in question) Array methods: .keys() .values() .entries() [Symbol.iterator] Object: .observe() (initially was planned for ES7, but was removed from the spec entirely on November 2, 2015) .is() .setPrototypeOf() .getOwnPropertySymbols() .getNotifier() (not es6, example … Read more