Sep 14, 2017 In this article we would be discussing Map object provided by ES6.Map is a collection of elements where each element is stored as a Key, value pair. Map object can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key,value pair in the same order as inserted.
by Alex Permyakov
When you read about Array.reduce and how cool it is, the first and sometimes the only example you find is the sum of numbers. This is not our definition of ‘useful’. ?
Moreover, I’ve never seen it in a real codebase. But, what I’ve seen a lot is 7–8 line for-loop statements for solving a regular task where Array.reduce could do it in one line.
Recently I rewrote a few modules using these great functions. It surprised me how simplified the codebase became. So, below is a list of goodies.
If you have a good example of using a map or reduce method — post it in the comments section. ?
Free steam codes no survey or download. Let’s get started!
Well, this is the only one not about map/reduce/filter, but it’s so compact that it was hard not to put it in the list. Plus we’ll use it in a few examples too.
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
The some() method tests whether at least one element in the array passes the test implemented by the provided function.
The result of the first iteration is equal to : […[], …[1, 2, 3]] means it transforms to [1, 2, 3] — this value we provide as an ‘acc’ on the second iteration and so on.
We can slightly improve this code by omitting an empty array[]
as the second argument for reduce(). Then the first value of the nested will be used as the initial acc value. Thanks to Vladimir Efanov.
Note that using the spread operator inside a reduce is not great for performance. This example is a case when measuring performance makes sense for your use-case. ☝️
Thanks to Paweł Wolak, here is a shorter way without Array.reduce:
Also Array.flat is coming, but it’s still an experimental feature.
Let’s group and count the ‘age’ property for each item in the array:
Thanks to sai krishna for suggesting this one!
Instead of processing the whole array for finding a user by id, we can construct an object where the user’s id represents a key (with constant searching time).
It’s useful when you have to access your data by id like uTable[85].name
a lot.
Let’s create a list of existing users’ groups. The map() method creates a new array with the results of calling a provided function on every element in the calling array.
This one-liner looks quite tricky. We use the comma operator here, and it means we return the last value in parenthesis — acc
. Let’s rewrite this example in a more production-ready and performant way:
Here we don’t use spread operator — it creates a new array on each reduce() call, which leads to a big performance penalty: O(n²). Instead the old good push() method.
Think of it as processing each element with a given formula ?
Sometimes you want to print your array of objects with selected keys as a string, but you realize that JSON.stringify is not that great ?
JSON.stringify can make the string output more readable, but not as a table:
Let’s say we want to change John’s age. If you know the index, you can write this line: users[1].age = 29
. However, let’s take a look at another way of doing it:
Here instead of changing the single item in our array, we create a new one with only one element different. Now we can compare our arrays just by reference like updatedUsers users
which is super quick! React.js uses this approach to speed up the reconciliation process. Here is an explanation.
Less code than importing and calling the lodash method union.
The last one!
As an exercise try to implement difference (A B) of the arrays. Hint: use an exclamation mark.
Thanks to Asmor and incarnatethegreat for their comments about #9.
If you have any questions or feedback, let me know in the comments down below or ping me on Twitter.
Here are more articles I’ve written:
Production ready Node.js REST APIs Setup using TypeScript, PostgreSQL and Redis.
A month ago I was given a task to build a simple Search API. All It had to do is to grab some data from 3rd party…
Thanks for reading ❤️
JSON is a format for storing and transporting data.
JSON is often used when data is sent from a server to a web page.
* The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.
This JSON syntax defines an employees object: an array of 3 employee records (objects):
The JSON format is syntactically identical to the code for creating JavaScript objects.
Because of this similarity, a JavaScript program can easily convert JSON data into native JavaScript objects.
JSON data is written as name/value pairs, just like JavaScript object properties.
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
JSON names require double quotes. JavaScript names do not.
JSON objects are written inside curly braces.
Just like in JavaScript, objects can contain multiple name/value pairs:
JSON arrays are written inside square brackets.
Just like in JavaScript, an array can contain objects:
In the example above, the object 'employees' is an array. It contains three objects.
Each object is a record of a person (with a first name and a last name).
A common use of JSON is to read data from a web server, and display the data in a web page.
For simplicity, this can be demonstrated using a string as input.
First, create a JavaScript string containing JSON syntax:
Then, use the JavaScript built-in function JSON.parse()
to convert the string into a JavaScript object:
Finally, use the new JavaScript object in your page:
You can read more about JSON in our JSON tutorial.