Tag: ecmascript 6

ES6 How to get _this inside class, inside another context

This question already has an answer here: How to access the correct `this` inside a callback? 4 answers

Format date in ES6

I am formatting a Date, with not momentjs or any other library, just pure JS. And I want to know if there is a way to simplify this with ES6 let currentDate = new Date(); const videosInformation = { time: currentDate.getHours() + ‘:’ + currentDate.getMinutes(), date: (currentDate.getMonth() + 1) + ‘/’ + currentDate.getDate() + ‘/’ […]

Wrap a console.log with infinite optional params

I’m trying to create a custom logger which just wraps a console.log but always adds something to the start which makes it absolutely clear that the log is coming from my plugin. I have come up with the following code: var log = function(param1, param2) { if (typeof param3 !== ‘undefined’) { console.log(‘[MyPlugin]’, param1, param2, […]

Find if an element exists in Javascript array

I am trying to find if an element exists in Array with a name. Couldn’t figure out, how to achieve the same let string = [{“plugin”:[“”]}, {“test”: “123”}] console.log(string); console.log(string instanceof Array); //true console.log(“plugin” in string); //false

ECMAScript 6 arrow functions

var getTempItem = id => ({ id: id, name: “Temp” }); I know the above arrow function is equivalent to: var getTempItem = function(id) { return { id: id, name: “Temp” }; }; But I’m a bit confused about the following const Todo = ({ onClick, completed, text }) => ( <li onClick={onClick} style={{ textDecoration: […]

Why can't I return an arrow function?

This question already has an answer here: Arrow function vs function declaration / expressions: Are they equivalent / exchangeable? 2 answers

Reverse an ES6 Map

Suppose I have created a Map object like this Map {“a” => “apple”, “b” => “banana”}: m = new Map([ [“a”, “apple”], [“b”, “banana”] ]); Now I want to reverse it and get Map {“b” => “banana”, “a” => “apple”} I see the only way to do it as follows: new Map(Array.from(m.entries()).reverse()); which doesn’t look […]

Testing React: Target Container is not a DOM element

I’m attempting to test a React component with Jest/Enzyme while using Webpack. I have a very simple test @ import React from ‘react’; import { shallow } from ‘enzyme’; import App from ‘./App’; it(‘App’, () => { const app = shallow(<App />); expect(1).toEqual(1); }); The relative component it’s picking up is : import React, { […]

How to chain async methods in javascript ES6 classes

I want to chain methods from a class. I have o problems with synchronous methods, but I don’t know how to do it with asynchronous methods. For example, this class: class Example { constructor() { this.val = 0 } async () { setTimeout(() => { this.val += 1 return this }, 5000) } sync () […]

Get the different of arrays in ES6?

So I have two arrays: const allLanguages = [ ‘ES’, ‘EN’, ‘DE’ ] const usedLanguages = [ { id: 1, lang: ‘EN’ } ] What’s the quickest way to produce a new array which is the difference of these two? In old school JavaScript, you’d have to do a for loop inside another for loop, […]