quma
quma

Reputation: 5719

get Key and Value of HashMap in Javascript/AngularJS

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.

Upvotes: 2

Views: 21021

Answers (2)

mostafa tourad
mostafa tourad

Reputation: 4388

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']);

Upvotes: 2

brk
brk

Reputation: 50326

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)

Upvotes: 3

Related Questions