3

I am looking for a way how we can implement HashMap functionality in javascript. I found map() as global object which has been introduced in es6 using which we can do the same.But making a a value as an array doesn't seems to be working.Can anyone help me for the same.
Map-JavaScript | MDN

Below is the snippet of the sample code.

    var map1 = new Map(); 
    var names=['abc','bcd','abc'];
    for(var i=0;i<name.length;i++){
        if(map1.has(name[i]))
            map1.set(name[i],map1.get(name[i]).push('B'));//if key is already there append B
        else
            map1.set(name[i],['A']);//if key is not present append A
    }

    console.log(map1);

I am expecting the value of the key abc as ['A','B']

Actual Result : Map { 'abc' => 2, 'bcd' => [ 'A' ] }

Expected Result : Map { 'abc' => ['A','B'],'bcd' => ['A']}

2
  • Also note that .push() returns the new length of the array, not the array itself. Commented Aug 27, 2019 at 17:35
  • 1
    this is because you are assigning the result of map1.get(name[i]).push('B') and that result is the new length of the array. You don't need to assign anything - arrays in JS are actually like ArrayLists in Java and can grow normally. Also, since you're storing the array reference, any modifications to the array will be "shared" with the map where it's at. Since you're only modifying the same object. Just do map1.get(name[i]).push('B')) without doing map.set at the same time. Commented Aug 27, 2019 at 17:35

2 Answers 2

2

The Array.prototype.push returns the size of the new array not the array itself after inserting a new element.

Just do a Map.get(key) to get the previous array reference and push inserted key you don't need to do a Map.set(key) again:

var map1 = new Map(); 
const names = ['abc','bcd','abc'];

for(let i=0; i<names.length; i++){
   if(map1.has(names[i])){
      map1.get(names[i]).push('B');
   }
   else{
      map1.set(names[i], ['A'] );
   }
}

console.log(...map1.entries());

Also name refers to the global variable window.name so do not use that as a variable name:

console.log(Object.is(name, window.name));

Sign up to request clarification or add additional context in comments.

Comments

0

Don't use name as variable name if you're using var, it will conflict with window.name, or just use let name, or use any other variable name,

Also push returns length of the array, so when you do map1.set(key, x.push('B')) it will set the length as value to that key not the array, so use concat or you can use ... spread syntax

let map1 = new Map();
let name = ['abc', 'bcd', 'abc'];
for (let i = 0; i < name.length; i++) {
  if(map1.has(name[i])){
    map1.set(name[i], map1.get(name[i]).concat('B'))
  }else{
    map1.set(name[i], ['A'])
  }
}

console.log([...map1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.