I’m trying to format postcode entries in a form correctly so that they have a space in the correct location, which is always three characters from the end. E.g.: AA1 1AA A1A 1AA AA11 1AA A1 1AA However, if I type a series of characters into the relevant field it is inserting spaces every other character…
Tag: javascript
How can i recursively group children objects under parent in an array of objects in JavaScript?
I have an array of topics each linked to a parent topic. So, basically what I want to do is form an array with all the children topics nested under the parent. So if I had an table of topics looking like this: id name parent_id 1 Topic 1 null 2 Topic 2 1 3 Topic 3 2 4 Topic
Why does this always return false no matter which numbers I plug into the function
Answer It doesn’t. But to properly compare the difference, you should keep in mind that subtraction may result in a negative number – in which case you may get something like if (1 > -8) – the magnitude of the difference is larger, but if it’s negative, it’ll still fulfill the…
ReactJS counter or timer with Start or Stop Button
Need help to understand in useEffect, if I don’t put counter and timerCheck in useEffect dependency then what it will effect here. And if I put timerCheck dependency in useEffect then counter increasing 100 time faster Also how can i run this code without any error or warning code in codesandbox Answer …
Comparing two nested arrays of objects, and exclude the elements who match values by “id” into new array in JS
I have two arrays of objects, i need to match arr2 names to arr1 by matching id’s Answer You could create a Map from arr2, keyed by id and with as value the corresponding object. Then map arr1, looking up the object in the Map, both for the object itself and the children:
Filter an array nested within an object
I was able to find many sources saying how to filter nested arrays, but all of these referred to arrays within arrays. In my case, there’s an array nested within an object and I can’t figure out how to deal with it. The source array is: How to filter the nested array so that the resulting object l…
Getting all the children of a tree javascript object as a flat array
I have a tree-based javascript object. So, im trying to get all of them as a flat array, like this: What options do i have to get them all without doing some dirty spaguetti code? I tried using array.forEach() but it naturally requires doing another iteration inside it, and surely will require it again while …
Prevent node.js from converting a nested object with numeric keys into array
When i have an object with a nested object within, whose keys are numbers and send it to my node.js server, then the nested object is converted to an array. How can i prevent this ? Client: Server: { new: [ ‘Test’, ‘Hello’ ] } When i add a not numeric key, everything works fine. Also w…
React native, the useEffect on a object state not working
Here is the code before the render So the state of HHDays stays null and the render shows an error on the frist {HHDays.allWeek.from} TypeError: null is not an object (evaluating ‘HHDays.allWeek’) Answer useEffect is called after the component was rendered. So when the code reaches that line the f…
MongoDB attribute to array migration script
I’m trying to make a migration for a database, which has the following structure as of now: There have been some changes in how we need to handle a site, having now the possibility to have multiple sites, and not only one. So I need to change the DB to look like this: Is there a way to change the