ecmascript-6

How can I make redux dispatch an action as well as return a promise?

时光毁灭记忆、已成空白 提交于 2021-01-27 14:10:40
问题 Here is my function in songAction.js export function createSong(title, url, token) { axios.defaults.headers.common['Authorization'] = token return function (dispatch) { axios.post('http://localhost:8080/api/song/create', { title, link: url }) .then((response) => { console.log('the response was', response) if(response.data.success){ dispatch({type: "CREATE_SONG_FULFILLED", payload: response.data.song}) } else { dispatch({type: "CREATE_SONG_REJECTED", payload: response.data}) } }) .catch((err)

JavaScript objects array filter by minimum value of an attribute

隐身守侯 提交于 2021-01-27 13:46:51
问题 I need to filter this object array by minimum value of 'rest' attribute. This is an one way to do it. Is there any other ways ? 'data' variable is a result of chained function. Is there any other way to do this without calling 'data' variable again inside Math.min() function. let data = [ { size: 5, qty: 2, rest: 0 }, { size: 2, qty: 5, rest: 0 }, { size: 1, qty: 10, rest: 0 }, { size: 3, qty: 3, rest: 1 }, { size: 4, qty: 2, rest: 2 } ] let result = data.filter(e=> e.rest === Math.min(..

Remove duplicate objects from an array but merge nested objects

陌路散爱 提交于 2021-01-27 10:54:53
问题 Currently have an array of objects which contain game releases. However game releases can happen on multiple platforms and these appear as separate objects within the array. I'm looking to remove duplicate games by comparing the game id but merge the platforms object I have tried using the reduce function which successfully removes duplicate objects by game id but I'm unable to adapt this to merge platforms const filteredArr = data.reduce((acc, current) => { const x = acc.find(item => item

How to create a vanilla JS routing for SPA?

假装没事ソ 提交于 2021-01-27 07:56:30
问题 I'm creating a web app with NO frameworks/tools/libraries. Why? It doesn't matter. All Vanilla JS. I'm doing it more of 'React' style. I'm wondering, how can I call the view that are in my views/pages/dashboard.js and display that when the user click the dashboard nav link? Perhaps the sub-nav items. What if the user is in the github folder on profile, how would I display that in the url as well? How can I create a routing for this? I've read articles an watched YT videos but can't seem to

How to create a vanilla JS routing for SPA?

北慕城南 提交于 2021-01-27 07:54:44
问题 I'm creating a web app with NO frameworks/tools/libraries. Why? It doesn't matter. All Vanilla JS. I'm doing it more of 'React' style. I'm wondering, how can I call the view that are in my views/pages/dashboard.js and display that when the user click the dashboard nav link? Perhaps the sub-nav items. What if the user is in the github folder on profile, how would I display that in the url as well? How can I create a routing for this? I've read articles an watched YT videos but can't seem to

Trying to make a simple Axios mock with jest

醉酒当歌 提交于 2021-01-27 07:33:11
问题 I am trying to understand this example, so the first thing I am trying is removing his axiosConfig.js , so the example looks more like the current case I would like to solve. However I get this error - Expected + Received Object { + "baseURL": "https://jsonplaceholder.typicode.com/albums", "method": "get", "url": "/3/photos?_limit=3", }, Number of calls: 1 39 | const photos = await getPhotosByAlbumID(3); 40 | expect(axios.request).toHaveBeenCalled(); > 41 | expect(axios.request)

Trying to make a simple Axios mock with jest

﹥>﹥吖頭↗ 提交于 2021-01-27 07:32:23
问题 I am trying to understand this example, so the first thing I am trying is removing his axiosConfig.js , so the example looks more like the current case I would like to solve. However I get this error - Expected + Received Object { + "baseURL": "https://jsonplaceholder.typicode.com/albums", "method": "get", "url": "/3/photos?_limit=3", }, Number of calls: 1 39 | const photos = await getPhotosByAlbumID(3); 40 | expect(axios.request).toHaveBeenCalled(); > 41 | expect(axios.request)

ES6 group array objects by field type

点点圈 提交于 2021-01-27 06:22:27
问题 Is there a terse es6 functional way to group items by type in that it's immutable? accounts.json [ {"account": {"name": "Bob's credit", "type": "credit", "id": "1"}}, {"account": {"name": "savings", "type": "savings", "id": "2"}}, {"account": {"name": "vacation savings", "type": "savings", "id": "3"}}, {"account": {"name": "son's savings", "type": "savings", "id": "4"}, {"account": {"name": "wife's credit card", "type": "savings", "id": "5"} ] Expected [ {"savings": [ {"account": {"name":

Javascript : Modifying Prototype doesn't affect existing Instances [duplicate]

五迷三道 提交于 2021-01-27 04:29:07
问题 This question already has an answer here : Overwriting and Extending Prototype (1 answer) Closed 3 years ago . I created 2 instances of a prototype, changed a function in prototype, changes reflected in both the instances (Great). However, when I modified the prototype by removing the function, the function still existed for the existing instances. function A() { this.name = "cool"; } A.prototype = { howCool: function() { return this.name + "er"; } }; var a1 = new A(), a2 = new A(); a1.name =

Block-level variable redeclaration with var vs. let/const

那年仲夏 提交于 2021-01-27 04:10:47
问题 Part 1 Given this example: var number = 10 { var number = 42 } console.log(number) // 42 Why does line 4 not throw an Uncaught SyntaxError: Identifier 'number' has already been declared ? It works with let / const because of block scoping (although the output is, of course, 10 not 42 ), but how come it works with var ? Part 2 Compare this to the following, which works with var : var number = 10 var number = 42 console.log(number) // 42 but doesn't with let : let number = 10 let number = 42 //