0
var category = [{
    key: 'a',
    value: 'avinash'
}, {
    key: 'b',
    value: 'balaji'
}, {
    key: 's',
    value: 'satish'
}, {
    key: 'd',
    value: 'dheeraj'
}, {
    key: 'p',
    value: 'poonam'
}, ];

Is this is Right Way to create? If not,Can any1 suggest me?

3
  • 1
    How else would you create it? Also, you're much more likely to get answers if you use proper formatting and don't use things like "any1". Commented May 16, 2012 at 6:54
  • 2
    I did just notice, however, that you have an extra comma after the last object literal. Commented May 16, 2012 at 6:55
  • ref: en.wikipedia.org/wiki/Trie Commented May 16, 2012 at 6:55

1 Answer 1

4

yes, it is one way of doing it... but i think a simple object will do the job just fine:

var category = {'a': 'avinash',
                'b': 'balaji',
                's': 'satish',
                'd': 'dheeraj',
                'p': 'poonam' };
Sign up to request clarification or add additional context in comments.

5 Comments

However, the OP's method provides for much more flexability in the key vaules, such as functions and objects (may not be required though). It's also safer where the key is not a valid identifier and dot notation is being used (e.g. key:'my-key' must be category['my-key'] not category.my-key.
@RobG how exactly do you access a value by key using dot notation in OP's array object?
Thnx Users. .I got my ans if i doing my above mentioned way or by deathApri; way. .
@deathApril—You can't unless there is an index that gives the position in the array. My point was that category[i].key can have a wider range of values than is allowed for property names, and that depending on the value of key, you can't use dot property access. Not saying your approach is bad, just more restrictive. It may not matter to the OP, but worth providing the whole picture.
@RobG what i tried to say is that dot property access is absolutelly useless for a dictionary that does not support access to values by a key, where you have to access both key and value throught an unknown index... of course, the OP's way can be a better solution for a particular problem (even if i can't think of any dictionary application where you won't need to access values by keys)

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.