Skip to content

Tag: ecmascript-6

How to add cumulative values in a JSON object in ES6?

I’m trying to add cumulative values in my JSON object. When I tried using reduce it’s summing up all the values of particular key value. How can I achieve my desired output? Sample JSON required output Answer Have a variable to hold the cumulative sum; map each original item to a new one, deconstr…

Import and execute all files on folder with es6

I am migrating some node ES5 node code to Typescript. I need to port these two pieces of code where I iterate through all the files in a directory and call the default function these files are exporting. How would I do that in Typescript or ES6? models/index.js index.js Answer You can do this with dynamic imp…

Defining listener function inside function

When I define a function outside the function, I cannot access the glide parameter: Hence, I have did something like that: I want to know if it is hack or good practice? Is there more convenient way for doing this. Answer Having the function outside is better. Mainly for readability and testing, but if your f…

Concatenating child array to parent

Right now I have an array of object with children that also have an array of objects. I want to extract the children array and concat them to the parent array like below. What are someways to do this. I’m currently looping though the child array creating a new array checking if it’s not empty. It …