ecmascript-7

getting difference object from two objects using es6

不打扰是莪最后的温柔 提交于 2019-12-11 19:10:40
问题 Im trying to figure out whats the best way to get an intersection object between two objects using es6. by this i mean something like: a = {a:'a',b:'b',c:'c', d:'d'}; b = {a:'a',b: '1', c:'c', d:'2', f'!!!'} // result I want: c = getDifference(a,b) //c is now: {b:'1', d:'2'} Is there a short way to do this using es6, or do I need to iterate over the a object using for(in) with Object.keys() and compare, assigning intersections to c? (a,b) => { const c = {}; for(const _key in Object.keys(a)){

Is there a shorthand for this in ES6/ES7?

梦想的初衷 提交于 2019-12-11 01:20:21
问题 I'm having a bit of a brainfart. Is there a shorthand for this in ES6/ES7? res.locals.hello = hello I've tried a few different combinations but can't get anything to stick. 回答1: I don't believe there is a shorter way to arbitrarily attach a new key to an object, and automatically assign a reference with the same name. However, during the construction of your locals object, you can simply provide the handler: let res = { locals: { hello } }; This is effectively the same as: let res = { locals:

Find all matching elements with in an array of objects [duplicate]

社会主义新天地 提交于 2019-12-10 15:14:06
问题 This question already has answers here : Find all objects with matching Ids javascript (2 answers) Closed last year . I have an array of objects I am searching within the array like this let arr = [ { name:"string 1", arrayWithvalue:"1,2", other: "that" }, { name:"string 2", arrayWithvalue:"2", other: "that" }, { name:"string 2", arrayWithvalue:"2,3", other: "that" }, { name:"string 2", arrayWithvalue:"4,5", other: "that" }, { name:"string 2", arrayWithvalue:"4", other: "that" }, ]; var item

Static method is undefined in ES6 classes with a decorator in reactjs

送分小仙女□ 提交于 2019-12-10 13:39:41
问题 I have an ES6 class with a decorator. It has a static method foo. However when I try to access the static method, its undefined. @withStyles(styles) class MyComponent extends Component { static foo(){ return "FOO"; } render(){ var x = MyComponent.foo; // x=undefined } } When I remove the decorator I can access the static method. Its no longer undefined. class MyComponent extends Component { static foo(){ return "FOO"; } render(){ var x = MyComponent.foo; // x=foo() } } Is there a workaround

How to match only those numbers which have an even number of `%`s preceding them?

与世无争的帅哥 提交于 2019-12-10 04:24:36
问题 I want to catch numbers appearing anywhere in a string, and replace them with "(.+)". But I want to catch only those numbers which have an even number of % s preceding them. No worries if any surrounding chars get caught up: we can use capture groups to filter out the numbers. I'm unable to come up with an ECMAscript regular expression. Here is the playground: abcd %1 %%2 %%%3 %%%%4 efgh abcd%12%%34%%%666%%%%11efgh A successful catch will behave like this: Things I have tried: If you have

Class decorators in ES7

自古美人都是妖i 提交于 2019-12-10 02:25:16
问题 I've been reading up on decorators in JavaScript, and think I'm getting the basic premise. Decorators are functions, they receive as a or several parameters what they should decorate, and return the result. But I came over a @withStyles decorated implementation in a React Boiler Plate project which I do not understand how works. import React, { Component, PropTypes } from 'react'; function withStyles(...styles) { return (BaseComponent) => class StyledComponent extends Component { static

Node exits before async function completes

巧了我就是萌 提交于 2019-12-10 02:13:41
问题 I have a function that returns a promise, and I am trying to await on it from within an async function. The problem is that the program completes immediately, instead of awaiting the promise. async-test.js: function doItSlow() { const deferred = new Promise(); setTimeout( () => { console.log( "resolving" ); deferred.resolve(); }, 1000 ); return deferred; } async function waitForIt( done ) { console.log( "awaiting" ); await doItSlow(); console.log( "awaited" ); done(); } waitForIt(() => {

ES7 Getting result from an array of promises using await generator

早过忘川 提交于 2019-12-10 00:40:36
问题 Given an array of promises, what's the idiomatic way to get the results in ES7? Here's what I want to do: async function getImports() { let imports = [System.import('./package1.js'), System.import('./package2.js')]; let promises = await* imports; let results = []; await promises.forEach(val => val.then(data => results.push(data))); //seems hacky console.log(results); // array of 2 resolved imports } The result is correct, but I'm still doing a forEach and a then to turn the resolved promises

Clean way to keep original variable and destruction at the same time

别来无恙 提交于 2019-12-09 16:11:59
问题 Is there a cleaner way to do this (with anything that is at least an ES draft and has a babel plugin, i.e., ES6, ES7, etc.): const { a, b } = result = doSomething(); Where I want to keep the overall result as one singular object, but also destructure it at the same time. It technically works, but result is implicitly declared (with an implicit var ), while I'd really like it to also be a const. I'm currently doing this: const result = doSomething(); const { a, b } = result; Which again works,

(_.merge in ES6/ES7)Object.assign without overriding undefined values

安稳与你 提交于 2019-12-08 15:55:14
问题 There is _.merge functionality in lodash. I want to achieve the same thing in ES6 or ES7. Having this snippet: Object.assign({}, {key: 2}, {key: undefined}) I want to receive {key: 2} . Currently I receive {key: undefined} This is NOT a deep merge. Is it possible? If yes then how to achieve that? 回答1: You can't achieve that with a straight usage of Object.assign , because each next object will rewrite the same keys for prev merge. The only way, to filter your incoming objects with some hand