The array find method and alternatives in vanilla javaScript

A long time ago I wrote a post on the lodash find method that is a way to go about finding a single element in an array. Lodash might still not be a dead library just yet, but I have to say that for the most part I am just making use of native javaScript features to do much of what can be done with lodash. One such method that might come to mind is the native array find method of the array prototype I native javaScript. There is also the array find index method that works just the same as the array find method only it will return an index rather than a value.

In many respects the array find method is just like the lodash fine method. There may be some talking points as to why the native array find method might not be a drop in replacement for the loadash fine method with respect to all use case scenarios. However there are many other native javaScript features that can be used to even over come those other situations in which the lodash fine method will work where the native array find method will not.

There is also not just the question of using the native array find method or the lodash fine method, there are many other lodash method of course, and there are also many tools to work with in core javaScript also. Most of the time I can not say that I use the find methods if any kind to find something actually oddly enough. There is also many other lodash methods such as the lodash filter method, as well as the native array filter method that can also be used to find not just the first element, but all elements that meet a condition. There is also the question of how to go about sorting the results of what I get from filtering an array. So in this post I will be touching base on the array find method, but I will also be looking into what other methods there are to work with when it comes to finding something.

Read More

Simple mine idle game javaScript examples

It has been a while sense the last time I made a simple javaScript project examples type post, so today I thought I would put something together real fast that I might put some more time into if I think it is something that is worth more time. The aim here is to not do anything fancy, just get together some javaScript code that will serve as a basic starting point for a simple idle game type project. This is not the first time I have made such a project, but maybe this time I will finally break old habits and continue working on this as a separate stand alone project rather than just a little javaScript code.

The main feature that I have in mine here is to just have a module that will create an instance of a kind of standard mine object. Each mine object will contain properties that defile what ores there are to obtain at the mine, and also the current state of a ship that will move back and forth from the mine. So the mine module is then use to create one of these mine objects, and update the state of the object when it comes to the position of the ship, the rate at which resources are mined, and create to a main home object. Speaking of the main home object I will also want at least that kind of module also that will be used to create and update that also.

Read More

Flatten an array with the flat method or alternatives

When it comes to working with arrays in javaScript, and I want to flatten an array of arrays into a single array of values, and I am working in a modern javaScript environment, then I can use the flat Array prototype method to do so. If for some reason I am using an old version of node that does not support Array.flat, or if I need better backward support then there are a wide rand of options when it comes to creating or finding alternatives to the array flat method also.

Read More

The string split method in javaScript

There are still many basic features of javaScript that I have not got around to writing a post on still such as the String Split prototype method. The string split method is simple enough in the sense that I can just call the method off of an instance of a string and pass a string that is a separator char that will be used to split the string into an array of sub strings. However there is maybe a bit more to write about when it comes to using the string split method in conjunction with many other native javaScript features. For example there is the question of how to go about converting an array of substrings back to a string, when it comes to that there is the array join method. Also there is what to do with an array of substrings once it has been split into an array, so I should make a few examples that involve the other array methods such as array map.

Read More

Array Reduce method in native javaScript

When it comes to the various javaScript array prototype methods the Array reduce method is one such method that will come in handy often with various tasks that have to do with arrays, and collections in general, in a javaScript programing environment. As the name suggests the main thing about the array reduce method is to reduce an array of elements down into a smaller array, or even a single primitive value such as a number or string. The way it works is by having a value in the body of the function that is given to array reduce method that is an accumulator variable which can have a starting value that is an array, number, string or any value that one would add to using data from the array elements to which this reduce method is call off of. There is then an additional argument in the body of a the function that is a current value of a current element along with this accumulator value, and other relevant values. It is then just a matter of working out what the additional logic should be in this reduce method when it comes to furnishing whatever the end result should be for the reduction. So then it is a good choice if I need to come up with some kind of sum of a whole bunch of values in an array of objects or something to that effect.

I have got around to writing a post on the lodash reduce method when I was writing a little content on that library, but I find myself using lodash less and less these days. Still there are some talking points as to why the lodash reduce method is not just a user space method that does the same thing as array reduce. For one thing the array reduce method is an array method while the lodash method is a so called collection method, which means that the method works with objects in general not just arrays. However when it comes to becoming very familiar with everything there is to work with in native javaScript alone, it is not always so hard to do the same with array reduce the trick is just coming up with an array first, or figuring out how to get array reduce to work with something that is not an array.

So I think it is called for now to write at least one post on the array reduce method in native core javaScript, and touch base on all kinds of little subjects that might come up as I work out a few basic examples and beyond. I will be starting out with basic hello world style examples of the method, and then process into various advanced topics, and ultimately some simple project examples at the bottom of this content.

Read More