Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Questions tagged [ecmascript-6]

The 2015 version of the ECMAScript specification, now a standard (ECMAScript 2015). Only use this tag where the question specifically relates to new features or technical changes in ECMAScript 2015.

3
votes
1answer
37 views

ES6 groupBy, partition, sort list of objects

I am trying to figure out an elegant way in ES6 to sort an array of objects based on specified values. Here's the scenario: const list = [ { "name": "john", "lastName": "smith" }, { "name": "tony", "...
1
vote
3answers
19 views

Isolate months from object array of dates and categorize according to months respective to the dates

let dates = { '2018/07/25': [['r','red'], ['b','blue']], '2018/07/26': [['a','apple'], ['o','orange']], '2018/08/01': [['d','deer'], ['l','lion']] ...
1
vote
0answers
30 views

Is it possible to intercept getters/setter calls in Javascript? (ES6)

I know it's possible in Javascript to have an object run a certain code when a non-existing method is called (with Proxies). But is it possible to do the same for non-existing properties? That is, I'...
60
votes
12answers
73k views

How to load external scripts dynamically in Angular?

I have this module which componentize the external library together with additional logic without adding the <script> tag directly into the index.html: import 'http://external.com/path/file.js' ...
1
vote
2answers
32 views

Can someone explain this es6 syntax to me?

Can someone explain the following statement? return state.map(todo => (todo.id === action.id) ? {...todo, completed: !todo.completed} : todo ) More specifically this line {....
0
votes
1answer
19 views

What module building strategy should I use for a Typescript module that could be used for either browser or node?

I'm going around in circles here - let me layout my understanding of modules as I currently understand it. Correct me if I'm wrong on any points. Modules are useful to avoid namespace pollution (ie. ...
17
votes
5answers
6k views

What are ES6 generators and how can I use them in node.js?

I was at a node.js meetup today, and someone I met there said that node.js has es6 generators. He said that this is a huge improvement over callback style programming, and would change the node ...
0
votes
1answer
158 views

what is _interopRequireDefault ?

I have seen an explanation on this website that say : _interopRequireDefault(): An ES6 CommonJS module is used as is (if it has a default export then it has a property named default). A normal ...
0
votes
0answers
10 views

how define value for default in ng-content?

How define value for default in ng-content ? look at this error enter image description here
11
votes
1answer
11k views

ES2015 module import and export syntax error

On using import export in ES6, I'm getting below error: SyntaxError: export declarations may only appear at top level I surfed to find how to fix this, but im unable to. Can anybody explain about ...
0
votes
1answer
43 views

Access destructured parameter by name

What I need: a way of accessing a destructured (or spread?) parameter by name. My code is as follows: open = ({ title = "Confirm", subTitle, link = {} } = {}) => { this.setState({ isVisible: ...
0
votes
2answers
103 views

emulator stuck at loading from 10.0.2.2:8081,what is issue?

I am working on a react-native project and I run it on emulator android(AVD manager) i have run adb reverse tcp:8081 tcp:8081 at cmd , but still my emulator stuck Whenever i am changing my project ...
0
votes
1answer
28 views

React Native weird flex behavior

I'm trying to create a header with an image so I write this: <View style={{ flex: 1, backgroundColor: "#FFFFFF", padding: 20 }} > <View style={...
0
votes
1answer
30 views

Javascript generator in a module/package

I ran into problem with generators and hopefully you can help me out. So, basically I separated/wrote my functions in separate modules (packages) so that it would be easier to update my application. ...
0
votes
0answers
29 views

Javascript onselectstart should not trigger click event [duplicate]

I would like to know, when a select event is used or when a click event is used. When using a click event, a function should be called, but when selecting the text, the click event should not trigger, ...
-1
votes
2answers
186 views

display the current npm version of my NodeJs app

I just want to display the version value of my package.json file within the footer of my app, but have no idea how to do that. I've read you could access those properties with process env object. Is ...
51
votes
5answers
44k views

How to import a json file in ecmascript 6?

How can I access a json file in Ecmascript 6 ? The following doesn't work: import config from '../config.json' This works fine if I try to import a JavaScript file.
0
votes
5answers
66 views

Which object does not have `hasOwnProperty` in Javascript?

With some value, hasOwnProperty calling throws error. Lets check the following code null.hasOwnProperty('bar') //error undefined.hasOwnProperty('bar') //error (0).hasOwnProperty('bar') //return ...
1
vote
1answer
35 views

Filter with multiple dynamic user inputs.How to do it more efficiently?

I have three inputs price that is coming from range input,name from text input and category from select.T tried using multiple if statements and using three separate functions as arguments.Which is ...
1
vote
1answer
42 views

Javascript / ES6 parse value from arrays of objects [on hold]

I want to write a generic parser which takes a value, and type and returns label of given value instead. Currently, this is my code: import a from "../constants/a" import b from "../constants/b" ...
310
votes
29answers
141k views

Private properties in JavaScript ES6 classes

Is it possible to create private properties in ES6 classes? Here's an example. How can I prevent access to instance.property? class Something { constructor(){ this.property = "test"; } } ...
0
votes
0answers
20 views

JS binded method is not using same instance [duplicate]

I will start with a snippet and then explain the issue. const authenticate = (cred, ad) => { console.log(this); // Window } class Model { constructor(mod) { const ad = {name: 'add'};...
-1
votes
0answers
31 views

I can not fetch food number in vue2.9

In my project, I use vue.js 2.9.5 and es6. I have created a cartcontrol.vue file, here is the code: <template> <div class="cartcontrol"> <transition name="move"> <...
0
votes
2answers
17 views

Timed retry on axios failure

I am using axios in the node module that I am developing for private use. On network failure, I want to perform retries in certain intervals (like exponential backoff). I have done this in my other ...
0
votes
1answer
11 views

Why does input parameter has an “object equal another object ” passed into

I am confused why the input param of the code below has { cabinet = 'spruce', distortion = '1', volume = '0' } = {} passed in. Does it mean all new objects created off of this class contain these ...
0
votes
1answer
18 views

Enabling disabled fieldsets under certain radio button selection contitions

Here’s what I want to achieve: Remove disabled attribute from the baz fieldset only after both of these conditions are met: If one or two is checked. After any bar radio input is checked. ...
3
votes
2answers
62 views

Are JavaScript iifes (Immediately-invoked function expressions) still relevant?

My understanding of the concept is that iifes allow you to simulate a "private" scope which prevents the global scope from becoming cluttered. Now that ECMAScript 6 has seen fairly widespread ...
2
votes
4answers
227 views

Calculate percentage increase using math formula

I have a function that calculates percentage increase of 2 numbers: const 1st_num = 50 const 2nd_num = 100 percentage = ((1st_num - 2nd_num) / 1st_num) * 100 // -100 It seems correct but what if ...
23
votes
2answers
7k views

How to correctly use ES6 “export default” with CommonJS “require”?

I've been working through Webpack tutorial. In one of the sections, it gives the code example that contains one line of essence to this question: export default class Button { /* class code here */ } ...
0
votes
2answers
31 views

How to deeply copy objects without replacing the entire property in ES6/Javascript [duplicate]

I want to deeply copy all missing fields into the object shown by the example code below. Is there a quick es6 shortcut for deeply copying the missing properties in the object? I tried using Object....
0
votes
2answers
22 views

Creating a communication channel between components in Angular

So I have two components, CompA and compA5 which are 3 or 4 levels apart, I want to create a communication channel between the components. Lets say from component CompA i want to send event to compA5 ...
0
votes
1answer
39 views

spy on method call in componentDidMount

I want to test some custom methods that get called in the componentDidMount life cycle method of a React component. componentDidMount() { getData().then(res => { console.log(res); ...
4
votes
2answers
4k views

SyntaxError: Unexpected token static

I'm currently trying to evaluate different testing frameworks that work with React, and it turns out that Jest is on my list. However, I'm trying to use static properties outlined here: https://github....
-3
votes
3answers
26 views

using await on global scope without async keyword

I am trying to do something like this on global scope in nodejs REPL. As per my understanding both the following statements are valid. see docs let x = await Promise.resolve(2); let y = await 2; ...
24
votes
6answers
2k views

Is there a functional way to init an array in JavaScript ES6?

I finally gave up and wrote a for loop to initialize a simple array of objects where each object has an incremented counter (id) as an attribute of the object. In other words, I just want: var ...
3
votes
4answers
47 views

ES6 .filter() that will return an array of objects WITHOUT certain strings

I'm able to successfully return an array based on if it contains a tag that equals === 'Featured'. Here is the code: getFeatured() { return blogPosts.filter((item) => ( item.tags.some((...
1
vote
2answers
119 views

Javascript Objects - when filter, my object's keys become a list of ordered numbers

I'm working on filtered an object. The filtration works great but my object's keys are tansformed in an ordered list of key. Here my original object: 5b7973bdce3eb938a6de86a3 : {addAt: ...
0
votes
5answers
27 views

Add conditional to object using push method

I'm wondering if there is a way to add a conditional in the array push method? I would only like the property 'provinceCodes' to be added to the object when it exists. Instead of doing this in the ...
1
vote
2answers
35 views

JS: Null-Safe Array Concat

Given: var a = [1, 2, 3] var b = null I need to: var c = [...a, ...b] .. but that does not work when a or b is null of course. So in this example b should just not be added, resulting in c = [1, ...
0
votes
1answer
29 views

React Navigation navigate to new screen from button in custom header

I'm using React-Navigation and i created my custom header. I used: navigationOptions: { header: <Header /> } Which <Header /> is imported from the path. I have an TouchableOpacity ...
0
votes
3answers
27 views

Making multiple http calls inside foreach loop async

Lets say I have a foreach loop Object.keys(this.treeMap).forEach(id=>{ this.getSelectionById(id); }) this.processAfterHttpCalls(); and getSelectionById method makes an http call ...
3
votes
2answers
55 views

How to chain async/await using for data that depends on the first call

Let's say I have an async/await that calls an API that fetches all users. async function getUsers() { const users = await Api.getAllUsers() return users.map(user => { return { id: ...
0
votes
3answers
41 views

JS/ReactJS - Assigning a new variable changes the original

I have a react app with an array formatted like so: var themes = [ { id: 1, name: 'Light', }, { id: 2, name: 'Dark', } ]; I have a method inside my React Component that adds ...
122
votes
5answers
61k views

Access to ES6 array element index inside for-of loop

We can access array elements using a for-of loop: for (const j of [1, 2, 3, 4, 5]) { console.log(j); } How can I modify this code to access the current index too? I want to achieve this using for-...
3
votes
3answers
52 views

better jsx condition to order component

I have an icon position prop that decide whether it's placed on the left or on the right of the children. I have this working <List> {iconPosition === 'right' && ( <Text /...
0
votes
0answers
20 views

Array comprehension synthax with vue.js, quasar and ES6

I want to use array comprehension in my quasar project. Quasar version : 0.17.* Vue.js version : 2.9.6, which uses es6 (aka ES2015) When I use : let newList = oldList.map(function (item) { return ...
0
votes
1answer
15 views

How to access and use Imported Function in ES6

I'm having trouble accessing the imported labelsForCountryCode function. When passing 'US' into this function, I'm expecting to have the US object returned. I'm new to ES6 and not exactly sure what I'...
1
vote
3answers
67 views

How to create a new array of objects form object without duplicates ? (ES6)

I would like to create an array of all "department" from the "users" array without duplicate in ES6. I've tried with forEach, reduce, filter, without success... Users array: let users = [{ ...
0
votes
1answer
20 views

Overriding an exported function in a javascript package

I'm trying to override a single exported function in a small package. The function handles adding a class to an invalid form element. I want to move the class to the parent to allow for pseudo ...