The lodash map method

If you work with javaScript a lot like I do chances are you are going to be aware of many of the methods that are part of the Array prototype, one of which is Array.map. This array prototype method can be used to map over all elements in an array by passing a function as the first argument when calling Array map off of an instance of an Array. In this method that is passed to array map the value of a current element in the array is available as the first argument in the method that ias passed, and the value that is returned in this method will become the new value for that current element.

In lodash there is also the _.map method what works just like the native array map method when it comes to arrays, only it is a little more advanced when it comes to working with objects in general with javaScript. You see the load map method is one of the so called collection methods in lodash. What this means is that the method works well out of the box with both arrays and plain old objects by themselves. In this post I will be covering Array.map, and how it compares with what is in lodash.

Read More

The lodash _.zip method and other related topics

The lodash _.zip method can be used to zip some separate arrays into one array of arrays. It is one of several helpful methods in lodash for working with multi-dimensional arrays, as _.zip can be used as a way to create them. Another such method that is helpful with these sorts of arrays is the _.chunk method that can be used to make a multi deferential array from a single array, while _.zip can make them from two, or more arrays. In addition there is also the lodash flatten method that can flatten and array of arrays into a single array that should also be worth checking out if you are now aware of it just yet.

Read More

The lodash property method

The lodash _.property method is one of several methods in lodash that can be used to create a method that is to be used with another lodash method such as _.find or _.map. The lodash property method returns a method that when used with a lodash method like _.map will return a property of an element object in an array. So lets look at some simple quick examples of this method to see what the lodash property method is all about, and also if it is not that big of a deal to not even bother with it.

Read More

The lodash _.mixin method

The process of combining objects in lodash, or in javaSript in general actually can prove be a little tricky. There are the own properties of an object, it’s prototype object including any inherited objects, as well as even hidden properties in some cases that can be added by way of the Object.definePropery method.

When it comes to using lodash there is the idea of extending lodash with custom methods that are not a part of lodash out of the box. The lodash _.mixin method can be used to extend lodash when one wants to do this sort of thing, however it can also be used with another object also. So the lodash mixin method can be called with just one argument, and in that case the first argument should be an object of methods that are to be added to lodash. When used to two or more arguments the first argument becomes a target object other than lodash to extent with methods. It is one of many methods in lodash that can be used to combine objects, in some cases it might be useful so lets take a look at _.mixin.

Read More

The acorn javaScript parser

For some projects it might be required to parse javaScript, often doing so might be a task that is done my a javaScript engine, but sometimes I might want to parse javaScript with javaScript. A popular and well know project for doing just that is acorn which is one of many user space nodejs project that can be used for this sort of thing.

If you are new to javaScript and nodejs, or programing in general and you do not know what a parser is then maybe a brief definition of a parser is in order. I guess one way of define a parser is software that will take a string from of something and create a workable object from of it. For example say you have a text game program where a player can input a command like move up, that results in a player character in the game moving up. A parse would be the software that takes that text and creates an object maybe that contains an action property with a value of move, and a direction property with the value of up. That object is then in turn what is used to preform the action of moving up in the text only game.

Read More