简体   繁体   中英

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.

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) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM