Skip to content

Latest commit

 

History

History
248 lines (166 loc) · 8.73 KB

31-Map.md

File metadata and controls

248 lines (166 loc) · 8.73 KB

What is a Map ?

In JavaScript, a map is a built-in data structure that allows you to store and retrieve key-value pairs. It's similar to an object, but with some important differences:

  • A map can use any type of value as its key, not just strings or symbols like objects.
  • The keys in a map are ordered, so you can iterate over them in the order they were added.
  • Maps have methods for getting the size, checking if a key exists, adding and removing elements, and more.

Overall, maps provide a convenient way to manage collections of data in JavaScript.

How to Create new Map ?

To create a new map in JavaScript, you can use the Map constructor function. Here's an example:

const myMap = new Map();

You can also initialize the map with key-value pairs like this:

const myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2']
]);

To add a new key-value pair to the map, you can use the set method:

myMap.set('newKey', 'newValue');

To retrieve a value from the map using its key, you can use the get method:

const value = myMap.get('key1');

To iterate over the keys and values in the map, you can use a for-of loop:

for (let [key, value] of myMap) {
  console.log(key, value);
}

Note that maps are similar to objects, but with some key differences such as being able to use any type of value as a key (not just strings), maintaining the order of insertion, and having a size property to get the number of entries in the map.

How to add value in a map ?

To add a key-value pair to a JavaScript map, you can use the set() method. Here's an example:

let myMap = new Map();
myMap.set('key', 'value');

In this example, we create a new map called myMap, and then add a key-value pair using the set() method. The first argument is the key, and the second argument is the value.

You can also add multiple key-value pairs at once by passing an array of arrays to the Map constructor, like this:

let myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2']
]);

This creates a new map with two key-value pairs: 'key1' maps to 'value1', and 'key2' maps to 'value2'.

What is Chaining in map ?

Chaining in map in JavaScript refers to the ability to chain multiple map() method calls together to perform a series of transformations on an array. This allows for more concise and readable code by avoiding intermediate variables and making the data transformation process more explicit. The resulting array is returned after applying all the transformations.

What is Map get function ?

The Map.get() function in JavaScript is a method used to retrieve the value associated with a specific key in a Map object. It takes a single argument, which is the key of the value to be retrieved, and returns the corresponding value if it exists in the Map, or undefined if it does not.

What is Map has function ?

In JavaScript, the map() function is a built-in higher-order function that allows you to apply a given transformation function to each element of an array and return a new array containing the transformed elements. The original array remains unchanged. The syntax for using map() is as follows:

array.map(function(currentValue,index,arr), thisValue)

The first argument is the transformation function that takes three parameters: currentValue (the current element being processed), index (the index of the current element), and arr (the original array). The second argument, thisValue, is optional and can be used to set the value of this within the function.

For example, the following code uses map() to transform an array of numbers by doubling each number:

const numbers = [1, 2, 3, 4];
const doubledNumbers = numbers.map(function(num) {
  return num * 2;
});
console.log(doubledNumbers); // Output: [2, 4, 6, 8]

What is Map size function ?

In JavaScript, the Map.size property returns the number of key-value pairs in a Map object. It is a read-only property and can be accessed using the dot notation like myMap.size.

What is Map clear function ?

The Map.clear() function in JavaScript is a method that removes all key-value pairs from a Map object, making it empty. After calling the clear() function, the Map object will have a size of 0. The function does not return anything.

Explain Array as key of Map ?

In JavaScript, an array can be used as a key in a Map data structure. When an array is used as a key, it is converted to a string using the Array.prototype.toString() method, which returns a comma-separated string of the array's elements.

For example:

const myMap = new Map();

const myArray = [1, 2, 3];

myMap.set(myArray, 'Hello World!');

console.log(myMap.get(myArray)); // Output: "Hello World!"

In this example, myArray is used as a key for the myMap object, and the value "Hello World!" is associated with it. When we retrieve the value using the get() method and pass in myArray, it correctly returns "Hello World!" because the same array instance is being used as the key.

It's worth noting that since arrays are objects in JavaScript, they can also have properties added or changed. This can cause unexpected behavior when using an array as a key, especially if the properties are not taken into account when converting the array to a string.

Explain Iteration of Map ?

In JavaScript, iteration of Map can be done using methods like forEach, for...of or entries.

  1. Using forEach method: The forEach method executes a provided function once for each key-value pair in the Map object, in insertion order.

Example:

let myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');

myMap.forEach(function(value, key) {
  console.log(key + ' = ' + value);
});
// Output: 
// 0 = zero
// 1 = one
  1. Using for...of loop: The for...of loop allows you to iterate over the key-value pairs in a Map object in insertion order.

Example:

let myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');

for (let [key, value] of myMap) {
  console.log(key + ' = ' + value);
}
// Output: 
// 0 = zero
// 1 = one
  1. Using entries method: The entries method returns an iterator object that contains an array of [key, value] pairs for each element in the Map object, in insertion order.

Example:

let myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');

let entries = myMap.entries();

for (let entry of entries) {
  console.log(entry[0] + ' = ' + entry[1]);
}
// Output: 
// 0 = zero
// 1 = one

How to convert Object to Map ?

In JavaScript, you can convert an object to a Map by using the built-in Map constructor and the Object.entries() method. Here's an example:

const obj = { foo: 'bar', baz: 'qux' };
const map = new Map(Object.entries(obj));

In this example, Object.entries(obj) returns an array of key-value pairs from the object obj, which is then passed as an argument to the Map constructor to create a new Map named map.

Note that if the object has non-string keys or symbol properties, they will be lost when converting to a Map, since Maps can only have string or symbol keys.

How to convert Map to Aray

You can convert a Map to an array in JavaScript using the Array.from() method or the spread operator (...). Here's how:

Using Array.from():

const myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3']
]);

const myArray = Array.from(myMap);
console.log(myArray); // [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]

Using the spread operator (...):

const myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3']
]);

const myArray = [...myMap];
console.log(myArray); // [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]

Use forEach function on Map

In JavaScript, forEach() is a method that can be called on an array to loop over its elements and perform some action for each element.

On the other hand, map() is a method that creates a new array with the results of calling a provided function on every element in the calling array.

To use forEach() on a map() operation in JavaScript, you could chain them together like this:

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map(num => num * 2);

doubledNumbers.forEach(num => console.log(num));

In this example, the numbers array is mapped to create a new array doubledNumbers, where each element is the original number multiplied by 2. The forEach() method is then called on doubledNumbers to log each element to the console.