javascript size of map code example

Example 1: js map size

const map1 = new Map();

map1.set('a', 'alpha');
map1.set('b', 'beta');
map1.set('g', 'gamma');

console.log(map1.size);
// expected output: 3

Example 2: size of map with no elements

#include <iostream>
#include <utility>
#include <map>

using namespace std;

int main(void)
{
    map<int, int> m;
    cout << m.size();
}

//Output: 0

Example 3: nodejs map

let wrongMap = new Map()
wrongMap['bla'] = 'blaa'
wrongMap['bla2'] = 'blaaa2'

console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }

Example 4: map()

The map() method creates a new array populated with the results of calling 
a provided function on every element in the calling array.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

Tags:

Java Example