Tag: ecmascript 6

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, […]

Mixing ES6 Promises with JQuery Promises

I have used $q (Angular.js) and would often return promises within a .then call. The effect was that the next .then call would wait for the previous promise to finish. I am now using native es6 promises to try to ‘promisify’ a callback based library and I am unable to do so. The problem is […]

Is it possible to rename “function” in JS?

For instance can a write a function using ಠ(){} instead of function(){} Note: ಠ is a valid identifier according to ECMAScript 6 / Unicode 8.0.0. https://mothereff.in/js-variables

Declaring a function in ES6?

i wanted to “update” my javascript code to the new ES6 Standard, so I looked at how functions are now written and tried it out on a global function of mine, which reads like this in the “old” es5 function logMessage(message) { document.getElementById(“logs”).innerHTML = document.getElementById(“logs”).innerHTML + `<li class=”item-padding”> ${message} </li>` } now if I’m not […]