2

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.

2 Answers 2

3

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)

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

Comments

2

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

2 Comments

I would have an additional question: how to get the first key?
Try this Object.keys(myVar)[0]

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.