0

I am trying to implement simple hash map with key - value pair similar to java in javascript. I want to map index and corresponding page number, index as key and pagenumber as value.

for example --> index - 1 , pagenumber-10

How to implement this in java script?

3
  • Just use JS Object. e.g. a = {}, a['index']=1, a['pagenumber']=10. The example is just one way you can do this. There are at least 2 other ways to declare JS Object and assign attribute to it. Commented Jul 19, 2012 at 6:36
  • how to this dynamically. I need to add the index and corresponding pagenumbers ? Commented Jul 19, 2012 at 6:38
  • Sorry, didn't read your question, but you can just do: a[1] = 10. Commented Jul 19, 2012 at 6:40

1 Answer 1

1

you can use javascript objects, where you can have key - value parametres, e.g, : x = { index : 1, pagenumber : 10 }

you can have objects inside the objects, for example :

x = { a : { index : 1, pagenumber : 10}, b : { chapter : 1, pagenumber : 20 } }

and you can access this like below :

   x.a.index which gives you 1
   x.a.pagenumber which gives you 10
Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to add key value pairs dynamically into this object. similar to put functionality of java hash map? Answer works fine if we know the actual number of elements.
@user1103504 sure you can you do that. x['c'] = { "something" : "value"}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.