html - how to create a hash map so I can loop through or delete in javascript -
i tried creating hashmap in javascript
var map ={}; // key string values don't know when want access // values objects
i want able loop through map. want able delete map pair using key.
i tried bunch of different things saw online , none of worked.
what best way this?
the for-in
statement supposed that. if populate object, can loop through same-level properties such:
var obj = {a: 1, b: 2, c: 3}; for(key in obj){ console.log(obj[key]); } // output: // 1 // 2 // 3
now, delete key-value pair, you'd have use delete
operator. transforming previous example:
var obj = {a: 1, b: 2, c: 3}; for(key in obj){ delete obj[key]; } console.log(obj); // output: // {}
the syntax above standard javascript, supported since es1.
Comments
Post a Comment