ecmascript-6

Converting an Arrow Style function to “function” style

≡放荡痞女 提交于 2021-02-05 06:36:43
问题 I have a function like this: const jsonObject = {a: {b: 'c'}}; const x = 'a.b'; const properties = x.split('.'); const item = properties.reduce((obj, prop) => obj && obj[prop], jsonObject); console.log(item); // prints 'c; This function, dynamically traverses the jsonObject and prints the value. This works fine, but this style of declaration doesn't support my environment. So I wan trying to convert this to function style declaration something like this: const item = properties.reduce

Converting an Arrow Style function to “function” style

倖福魔咒の 提交于 2021-02-05 06:36:27
问题 I have a function like this: const jsonObject = {a: {b: 'c'}}; const x = 'a.b'; const properties = x.split('.'); const item = properties.reduce((obj, prop) => obj && obj[prop], jsonObject); console.log(item); // prints 'c; This function, dynamically traverses the jsonObject and prints the value. This works fine, but this style of declaration doesn't support my environment. So I wan trying to convert this to function style declaration something like this: const item = properties.reduce

Javascript Object destructuring and default parameters combined

北城余情 提交于 2021-02-05 06:32:24
问题 Today I came across the following syntax which I didn't recognize: const createUser = ({ age = 1, name = 'Anonymous', }) => ({ age, name, }); const defaultP = createUser({ age: 5 }); console.log(defaultP); I think it uses Object destructuring and default parameters in order to set defaults of the object which is send as an argument. The syntax threw me a bit off because normally I see object destructuring only in the following manner: let obj = { prop1: 1 } const {prop1} = obj; console.log

What is “…” (3 dots) in javascript?

二次信任 提交于 2021-02-05 05:32:09
问题 I learned about this thing from this post. function StoreMixin(...stores) { // what is "..." var Mixin = { getInitialState() { return this.getStateFromStores(this.props); }, componentDidMount() { stores.forEach(store => store.addChangeListener(this.handleStoresChanged) ); this.setState(this.getStateFromStores(this.props)); }, componentWillUnmount() { stores.forEach(store => store.removeChangeListener(this.handleStoresChanged) ); }, handleStoresChanged() { if (this.isMounted()) { this.setState

JSON.parse not having the expected behaviour

断了今生、忘了曾经 提交于 2021-02-05 04:59:47
问题 I'm trying to get a json request, sent by post , and do the JSON.parse on it. But this error happens: Uncaught SyntaxError: Unexpected token m in JSON at position 2 at JSON.parse () at :1:19 The code below reproduces the error: const string = '{ msg_reject: \'Rejeitado porque sim\', accept: 1, photo: \'FSADKJK23B1\' }' const json = JSON.parse(string) And that's the way I'm sending it in my post { msg_reject: 'Rejeitado porque sim', accept: 1, photo: 'FSADKJK23B1' } Is there something wrong in

JSON.parse not having the expected behaviour

余生颓废 提交于 2021-02-05 04:59:10
问题 I'm trying to get a json request, sent by post , and do the JSON.parse on it. But this error happens: Uncaught SyntaxError: Unexpected token m in JSON at position 2 at JSON.parse () at :1:19 The code below reproduces the error: const string = '{ msg_reject: \'Rejeitado porque sim\', accept: 1, photo: \'FSADKJK23B1\' }' const json = JSON.parse(string) And that's the way I'm sending it in my post { msg_reject: 'Rejeitado porque sim', accept: 1, photo: 'FSADKJK23B1' } Is there something wrong in

JSON.parse not having the expected behaviour

断了今生、忘了曾经 提交于 2021-02-05 04:59:03
问题 I'm trying to get a json request, sent by post , and do the JSON.parse on it. But this error happens: Uncaught SyntaxError: Unexpected token m in JSON at position 2 at JSON.parse () at :1:19 The code below reproduces the error: const string = '{ msg_reject: \'Rejeitado porque sim\', accept: 1, photo: \'FSADKJK23B1\' }' const json = JSON.parse(string) And that's the way I'm sending it in my post { msg_reject: 'Rejeitado porque sim', accept: 1, photo: 'FSADKJK23B1' } Is there something wrong in

Javascript sort items excluding some specific items

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-04 21:27:50
问题 I am trying to sort some items (sort a map) , I can sort it successfully, but I want to exclude some items based on it's attribute Right now I am sorting like this based on attribute - price return (product.attr('active') !== 'f' }).sort(function(pA,pB){ return pB.attr('price') - pA.attr('price'); }); I want to skip some items based on attr('product_id') so the listed product_id's wont be sorted based, and will be returned first. return (product.attr('active') !== 'f' }).sort(function(pA,pB){

Javascript sort items excluding some specific items

馋奶兔 提交于 2021-02-04 21:26:38
问题 I am trying to sort some items (sort a map) , I can sort it successfully, but I want to exclude some items based on it's attribute Right now I am sorting like this based on attribute - price return (product.attr('active') !== 'f' }).sort(function(pA,pB){ return pB.attr('price') - pA.attr('price'); }); I want to skip some items based on attr('product_id') so the listed product_id's wont be sorted based, and will be returned first. return (product.attr('active') !== 'f' }).sort(function(pA,pB){

Javascript sort items excluding some specific items

家住魔仙堡 提交于 2021-02-04 21:26:30
问题 I am trying to sort some items (sort a map) , I can sort it successfully, but I want to exclude some items based on it's attribute Right now I am sorting like this based on attribute - price return (product.attr('active') !== 'f' }).sort(function(pA,pB){ return pB.attr('price') - pA.attr('price'); }); I want to skip some items based on attr('product_id') so the listed product_id's wont be sorted based, and will be returned first. return (product.attr('active') !== 'f' }).sort(function(pA,pB){