Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript Map properties and methods

const user = new Map([
  ['First Name', 'Divine'],
  ['Last Name', 'Orji'],
  ['Age', 24],
  [false, 'Dance'],
]);

// PROPERTIES

// Map.prototype.size: This returns the number of items in a Map.

console.log(user.size)
// 4


// METHODS
// Here are some methods you can use with Map:

// 1. Map.prototype.set(key, value): Adds a new key: value entry to your Map.

user.set('Pet', 'Gary');
console.log(user);

/*
Map(5) {
  'First Name' => 'Divine',
  'Last Name' => 'Orji',   
  'Age' => 24,
  false => 'Dance',        
  'Pet' => 'Gary'
}
*/



// 2. Map.prototype.has(key): Checks if the specified key exists in your Map.

console.log(user.has('Age'));
// true



// 3. Map.prototype.get(key): Gets the value associated with the specified key,
// or returns undefined if the key does not exist.

 console.log(user.get('Pet'));
// Gary

console.log(user.get('House'));
// undefined



// 4. Map.prototype.delete(key): Deletes the specified key and its value,
// then returns true. If the key does not exist, it returns false.

console.log(user.delete('Age'));
// true

console.log(user);
/*
Map(4) {
  'First Name' => 'Divine',
  'Last Name' => 'Orji',   
  false => 'Dance',        
  'Pet' => 'Gary'
}
*/

console.log(user.delete('House'));
// false



// 5. Map.prototype.entries(): Returns an object with each key: value pair stored as an array.

console.log(user.entries());

/*
[Map Entries] {
  [ 'First Name', 'Divine' ],     
  [ 'Last Name', 'Orji' ],        
  [ false, 'Dance' ],
  [ 'Pet', 'Gary' ]
}
*/



// 6. Map.prototype.keys(): Returns an object with all the keys in your Map.

console.log(user.keys());
// [Map Iterator] { 'First Name', 'Last Name', false, 'Pet' }



// 7. Map.prototype.values(): Returns an object with all the values in your Map.

console.log(user.values());
// [Map Iterator] { 'Divine', 'Orji', 'Dance', 'Gary' }



// 8. Map.prototype.forEach(): Executes a callback function on each entry in your Map.

user.forEach((value) => {
  console.log(value);
});

/*
Divine
Orji
Dance
Gary
*/



// 9. Map.prototype.clear(): Deletes all entries in your Map.

user.clear()

console.log(user)
// Map(0) {}
Comment

PREVIOUS NEXT
Code Example
Javascript :: saving react code messing up in vsc 
Javascript :: pylint vscode disable max line length 
Javascript :: Get rid of white space around Angular Material modal dialog 
Javascript :: clear session on browser close javascript 
Javascript :: submit form with ajax 
Javascript :: javascript check if undefined or null or empty string 
Javascript :: string methods javascript 
Javascript :: filereader reactjs 
Javascript :: js match regex 
Javascript :: change version webpack-dev-middleware 
Javascript :: distinguishing positive numbers in javascript 
Javascript :: how to attach function to button sweetalert2 
Javascript :: add onclick javascript dynamically 
Javascript :: full month name using moment 
Javascript :: how to recognize an array in javascript 
Javascript :: javascript union two sets 
Javascript :: js convert number array to string array 
Javascript :: external css not working in jsp 
Javascript :: function prototype in javascript 
Javascript :: head component nextjs 
Javascript :: browseranimationsmodule browsermodule has already been loaded 
Javascript :: css react 
Javascript :: javascript charcode 
Javascript :: sanitize html in javascript 
Javascript :: jquery repeater 
Javascript :: get text in protractor 
Javascript :: reverse array elements in javascript 
Javascript :: post json example 
Javascript :: datatables and toggle not working 
Javascript :: expo react navigation 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =