Q16. What is MAPS in javascript?
Q17. How MAPS are different from any javascript object?
Q18. What are SETS in Javascript?
Q19. How is map.set is different from set in javascript?
Q20. What is spread operator in javascript?
--------------------------------------------------------------------------------------------------------------------------
Q16. What is MAPS in javascript?
Q17. How MAPS are different from any javascript object?
Q18. What are SETS in Javascript?
Q19. How is map.set is different from set in javascript?
Q20. What is spread operator in javascript?
--------------------------------------------------------------------------------------------------------------------------
Q16. What is MAPS in javascript?
Answer 16:
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.
A Map object can iterate its elements in insertion order - a for..of loop will return an array of [key, value] for each iteration.
console.log(map1);
https://www.geeksforgeeks.org/map-in-javascript/
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.
A Map object can iterate its elements in insertion order - a for..of loop will return an array of [key, value] for each iteration.
console.log(map1);
https://www.geeksforgeeks.org/map-in-javascript/
--------------------------------------------------------------------------------------------------------------------------
Q17. How MAPS are different from any javascript object?
Answer 17:
Map is a data structure which helps in storing the data in the form of pairs. The pair consists of a unique key and a value mapped to the key. It helps prevent duplicity.
Object follows the same concept as that of map i.e. using key-value pair for storing data. But there are slight differences which makes map a better performer in certain situations.
Few basic differences are as follows:
let arrAll = [2,3,4];
sumAll(...arrAll); // spread all elements
})();
Map is a data structure which helps in storing the data in the form of pairs. The pair consists of a unique key and a value mapped to the key. It helps prevent duplicity.
Object follows the same concept as that of map i.e. using key-value pair for storing data. But there are slight differences which makes map a better performer in certain situations.
Few basic differences are as follows:
- In Object, the data-type of the key-field is restricted to integer, strings, and symbols. Whereas in Map, the key-field can be of any data-type (integer, an array, even an object!)
- In the Map, the original order of elements is preserved. This is not true in case of objects.
- The Map is an instance of an object but the vice-versa is not true.
--------------------------------------------------------------------------------------------------------------------------
Q18. What are SETS in Javascript?
Answer 18:
Sets are a new object type included in ES6 that allow the creation of collections of unique values. The values in a set can be either simple primitives, such as strings and integers or more complex object types like object literals or arrays.
- Think of sets as an array with no repeated elements.
There are many instances where we have an array containing many elements that we want to convert to a unique array. While it can be done by brute force on the array, checking elements, using loops, distinct callback method, etc., the best method is using set().
let arrayOne=['1','2','1','2']
console.log(arrayOne)
// ['1','2','1','2']
let setOne= new Set(arrayOne)
for(elem of setOne){
console.log(elem)
}
//'1'
//'2'
// Another way of doing
let setTwo= [...new Set(arrayOne)]
console.log(setTwo)
// ["1" ,"2"]
--------------------------------------------------------------------------------------------------------------------------
Q19. How is map.set is different from set in javascript?
Answer 19:
Map.Set is way we can add elements in map type collection. Where as SET is a collection in itself different from MAP.
--------------------------------------------------------------------------------------------------------------------------
Q20. What is spread operator in javascript?
Answer 20:
1. Spread operator can be used to create shallow copies.
2. spread operator spread array into individual elements.
👀 [Quick Recap: By default primitive data types are copied by value while objects and arrays(they are objects in javascript) are copied by reference. Shallow copy and Deep copy are techniques to copy the objects and array by value. Shallow copy copies the top elements by value while nested elements by reference. ]
--create shallow copy --
int arr[] = [1,2,3];
int arr2[] = [...arr]; //shallow copy
int arr3[] = arr[]; // this will create a reference copy means if any chagnes are made in arr3, they will reflect in arr1 as well.
----Spread elements ----
(function(){
'use strict';
function sumAll (a, b, c){
console.log(a + b + c);
}
(function(){
'use strict';
function sumAll (a, b, c){
console.log(a + b + c);
}
let arrAll = [2,3,4];
sumAll(...arrAll); // spread all elements
})();
No comments:
Post a Comment