ecmascript-6

grunt-terser Giving SyntaxError: “VARIABLE_NAME” is redeclared

99封情书 提交于 2021-01-28 05:44:31
问题 I'm using grunt-terser to minify my es6 files. I have two files. file-1.js file-2.js In both files I have required a module with same variable name like this: const VARIABLE_NAME = require('MODULE_NAME'); Here is my grunt-terser task: terser: { main: { options: { compress: true, toplevel: true }, files: { './dist/app.js': ['file-1.js', 'file-2.js'] } } } When I run npx grunt terser I get the following error: Running "terser:main" (terser) task >> SyntaxError: "VARIABLE_NAME" is redeclared 来源:

Grouping based on unique key in array of object

白昼怎懂夜的黑 提交于 2021-01-28 05:39:38
问题 I am working on a small React project where I am stuck at this point. I need to make a grouping based on array object key. Below is the Array: let addOnFeatures = [ {"name": "feature1", id: 101, cost: 100}, {"name": "feature2", id: 102, cost: 200}, {"name": "feature1", id: 103, cost: 300}, {"name": "feature3", id: 104, cost: 40} ] I need to make a grouping based on name key so that every id related to common name should be displayed on its own tab. Expected output should be like

Extending Array / Proxy in ES6 correctly?

五迷三道 提交于 2021-01-28 05:07:12
问题 Currently trying to make a custom implementation of Array / Object (Would end up being pretty similar i suppose) and have stumbled upon an issue that is driving me crazy. As you can see, b is only an instanceOf Array, even though its created from custom class CachedArray, thus my custom function testPush is not defined, and i cant for the sake of everything find what is the issue. Using Nodejs 6 function _setKey(target, key, value) { console.log('Setting value', key, 'to', value); target[key]

How to expand single level nested hashes on the fly?

若如初见. 提交于 2021-01-28 05:01:06
问题 I have an object where a few of the keys are nested single level hashes. In this example only b is nested. const j = { a: 'A', b: { bb: 'BB', bbb: 'BBB', }, c: 'C' }; Question What I am looking for is a way to loop over the object and if a key is a nested object, then print its keys instead. a bb bbb c Does anyone know how to do that? 回答1: You can do this recursively: function printKeys(obj) { for (const [key, val] of Object.entries(obj)) { if (typeof val === "object") { printKeys(val); }

Should I declare a variable using LET globally for a function that runs constantly?

萝らか妹 提交于 2021-01-28 04:27:43
问题 Most relevant answers here do not refer to let , which is block scoped, but to var , which is hoisted. I can't seem to get a definitive answer to this. I have a variable declared globally that initializes once: let firePaused = false; and then a function in the keyboard handler that is running everytime I press a button: function actOnKeyPress() { if (rightPressed) { game.hero.rotate(game.hero.speed); } else if (leftPressed) { game.hero.rotate(-game.hero.speed); } if (!firePressed) {

ES6 array initialization

余生长醉 提交于 2021-01-27 21:21:51
问题 Very new to ES6. In ES5 I might do something like this function newArray(){ var data = []; for(var i = 0; i < 5; i++){ data[i] = "test data " + i; } return data; } x = newArray() How would I do this in ES6 ? What I've got below is in error getData = () => ({ let data = Array.from(new Array(5), (x, i) => "test data " + i) return { data } }) 回答1: You create wrong the function with ES6 getData = () =>{ let data = Array.from(new Array(5), (x, i) => "test data " + i) return { data }; } console.log

Client side convert png file stream into file object

孤街醉人 提交于 2021-01-27 19:30:03
问题 I get 'png image stream' from an API.The API uses gm(GraphicsMagick) to generate image file stream. In client side I am using axios to make API call. Successfully I am able to receive the response.In client side I have to convert this file stream into a file object. I use the below code for that. But it didn't help to get the actual image back. Help me to convert the file stream into a valid image file object in client side Code: const imageStream = response.data; const blob = new Blob(

TypeScript : Object Equality Comparison (Object Equals Object)

假如想象 提交于 2021-01-27 19:14:45
问题 I have found this (personally) convenient answer that fits my needs: https://stackoverflow.com/a/6713782/2678218 But since I am using TypeScript , I can have something like this with Generics : private equals<T>(x: T, y: T) { if (x === y) { return true; // if both x and y are null or undefined and exactly the same } else if (!(x instanceof Object) || !(y instanceof Object)) { return false; // if they are not strictly equal, they both need to be Objects } else if (x.constructor !== y

Unflatten JS object and convert arrays

空扰寡人 提交于 2021-01-27 14:43:25
问题 I have a function used to flatten objects like so: let object = { a: 1, b: [ { c: 2 }, { c: 3 } ] }; flatten(object) // returns { 'a': 1, 'b.0.c': 2, 'b.1.c': 3 } I need to unflatten objects, but also revert arrays to how they were. I have the following code: unflatten(obj) { let final = {}; for (let prop in obj) { this.assign(final, prop.split('.'), obj[prop]); } return final; } assign(final, path, value) { let lastKeyIndex = path.length-1; for (var i = 0; i < lastKeyIndex; ++ i) { let key =

Fetching multiple files using Promises and Fetch API javascript

雨燕双飞 提交于 2021-01-27 14:31:16
问题 I am updating my javascript skills with Promises, already have in place a library with XHR and callbacks to load and inject multiple files at once and only proceed if ALL of them succeeded. I am trying to use Promise.all() and Fetch API to get a similar functionality but can't make it work: console.log('All the promises are resolved', values); always triggers no matter if some of the fetch promises failed. I want to be able to execute the code below, and only proceed with nowInitialize