AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Convert to number javascript8/28/2023 ![]() Rescale(5, 0, 1 / 10, 0, 100) // result 50 is slower as need the / and * Your example convertValueToBoundary(5, 0, 10, 0, 100) // Output -> 50 ![]() const rescale = (val, oMin, invRange, min, max)=>(val - oMin) * (max - min) * invRange + min This would improve the performance if you were converting many values from the same range. The only optimization possible is to remove the division (multiplication is quicker than division). To further remove code noise use an arrow function and shorten the argument names const rescale = (val, oMin, oMax, min, max)=>(val - oMin) * (max - min) / (oMax - oMin) + min return (number - oldMin) * (newMax - newMin) / (oldMax - oldMin) + newMin return (((number - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin However I would never guess what this function did without some extra information The function name is too long convertValueToBoundary the conversion is implied and value is commonly shortened to val so valToBoundary is less noisy. Your naming and code style make the code very noisy. The conversion should be conducted without the use of any native language functions as I plan to use the same formula across different languages and platforms. Given a scenario where this needs to execute multiple times in rapid succession, it would be ideal to have this execute with the least amount of arithmetic operations required. Is this the optimal way to conduct this calculation? After a number of different attempts the above formula is the best I could come up with. Return (((number - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMinįormula newValue = (((oldValue - oldMinimum) * (newMaximum - newMinimum)) / (oldMaximum - oldMinimum)) + newMinimumįor instance, given the number 5 within a range of 0-10, we want to increase the numerical range to be between 0-100 instead. * int The original number converted to the new boundary.įunction convertValueToBoundary(number, oldMin, oldMax, newMin, newMax) * int newMax The new maximium value to convert to. * int newMin The new minimum value to convert to. More clearly, om(obj, mapFn, thisArg) has the same result as om(obj).map(mapFn, thisArg), except that it does not create an intermediate array, and mapFn only receives two arguments ( element, index) without the whole array, because the array is still under construction.I have a function that takes a specific number and a range, and re-scales it to the corresponding number in a different range. ![]() If the arrayLike object is missing some index properties, they become undefined in the new array.Īom() has an optional parameter mapFn, which allows you to execute a function on each element of the array being created, similar to map(). To convert an async iterable to an array, use omAsync().Īom() never creates a sparse array. ![]() To convert an ordinary object that's not iterable or array-like to an array (by enumerating its property keys, values, or both), use Object.keys(), Object.values(), or Object.entries(). array-like objects (objects with a length property and indexed elements).iterable objects (objects such as Map and Set) or, if the object is not iterable,.Object.prototype._lookupSetter_() Deprecated.Object.prototype._lookupGetter_() Deprecated. ![]() Object.prototype._defineGetter_() Deprecated. ![]()
0 Comments
Read More
Leave a Reply. |