There are three ways to create an array.
Accessing elements from the array:
Array elements have an index, index starts from 0 and goes to array.length -1. To access an element at any particular index use the following syntax arrName[index].
To add elements to an already existing array use array.push().
To find the number of elements in the array use array.length property.
To check if an element exists use array.indexOf() or array.includes().
To remove an element from an array, we can use array.splice() method. If you want to remove the last element you can use array.pop() method.
Splice modifies the existing array, to remove an element at particular index use array.splice(index, 1).
You can combine two arrays using array.concat() method.
Like Array, Set is also a collection of items, the difference being in a set a value will appear only once.
You can create a set as follows:
let set = new Set([1, 2, 3, 4, 5, 6, 6, 6])
set will contain 1,2,3,4,5,6
- To get the number of elements in the set use the size property of the Set (set.size).
- To add value use set.add(), returns the set.
- To remove an element use set.delete(value), to remove all the elements from the set use set.clear()
Set is useful when you need a unique collection of items if you used an array for the same task you will need to have extra logic that eliminates the duplicates.
Is a collection of key-value pairs, so is it the same as an object?
At the top level, they appear to be the same however, there are few differences.
Keys of an object can only be Strings, that's not the case for maps.
Keys are stored in insertion order in a Map.
You can also get the size of the Map with size property.
A Map is iterable, so you can directly use .forEach on Map. For objects, you will have to get the keys first to get that key's value.
Along with these, Map prototype has a few helper methods.
For example, to check if a key is present in the map you could do
You can create a Map as follows:
let map = new Map([['name', 'John'], ['age', '21']])
- To get number of elements in the Map use the size property.
- You can't access values from the map as you would do with an Object. You need to use .get() method on the map.
To add value to the map you need to use the .set() method.
Now that you know the basics of these data structures go ahead and try them out, implement something.
If you like this post, do share it.
Top comments (7)
In the third item of how to use an array you misspelled
I always hated that JS lacks a lot of cool data structures that you could implement but maybe not as performant as you could in other languages :/
Thank you for telling me about the typo :)
Yeah might not be performant, will definitely improve in the future though :)
Object keys can also be
Yes you are right they can be Symbol :) not numbers though, numbers are cast to strings so at the end they are strings :)
Ah you are right.
=). Great article, BTW!
stackare also available ootb and should be mentioned alongside with
unshift(). Sorry for the nitpick.
Yes you are right.