get Key and Value of HashMap in Javascript/AngularJS

16,078

Solution 1

This is an object in javascript and here you use number string as a key so to access the object values use this syntax myVar[key] ; look at the example below

var myVar = {"24":{"amount":2,"minutes":30},"32":{"amount":3,"minutes":30}}
console.log(myVar['24']);

Solution 2

You can use Object.keys & Object.values

var myVar = {
  "24": {
    "amount": 2,
    "minutes": 30
  },
  "32": {
    "amount": 3,
    "minutes": 30
  }
}
var getKeysArray = Object.keys(myVar);
var getValueArray = Object.values(myVar)
console.log(getKeysArray, getValueArray)
Share:
16,078
quma
Author by

quma

I'm a software developer in Java (Spring), JavaScript, AngularJS, Angular, TypeScript.

Updated on July 25, 2022

Comments

  • quma
    quma almost 2 years

    I have this HashMap in Frontend getting from Backend:

     var myVar = {"24":{"amount":2,"minutes":30},"32":{"amount":3,"minutes":30}}
    

    Does anyone know how I can get the keys and the values in Javascript/AngularJS? I have tried

    {{myVar.24}}
    {{myVar.next()}}
    

    but nothing works.

  • quma
    quma over 6 years
    I would have an additional question: how to get the first key?
  • mostafa tourad
    mostafa tourad over 6 years
    Try this Object.keys(myVar)[0]