Array Comprehension

Array Comprehension

A quick intro before we move on. This is my first blog post on hashnode and I am really excited about it. Please forgive any issues that may come up and please let me know about it.

The array comprehension is a method largely used in the python community. It is a form of array manipulation to create an output from an already created data. This method is useful for sorting, filtering, looping and all forms of manipulations possible in arrays. This method was first introduced to the Ecmascript community on the ES7 release but has been withdrawn in favour of other functions like map, filter, sort and so on.

The syntax for this method is: [expression for(var in object) if(condition)]. Yes, I know it is a bit strange for JS developers. I will break it down. The expression indicates your intention for the array, that means it shows what data you intend to collect, maybe an array of multiplied values or only positive values or values in descending or ascending order, the for loop is the iterator in charge of looping through an iterable object be it array or object. P.S an array is also an object in its core and before I forget, the for loop does not have a body. It is evaluated and returned as is. No loop body and then the if condition is the statement to evaluate your truthy values, for example, if item is a fruit return it. The if condition is not compulsory and array can be returned without it.

This method no longer has support but it is a nice to know.

carbon(1).png