Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get value by key WITHOUT looping

I have a variable in jquery as follows:

var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };

Is there a way to get value by key WITHOUT iteration.

In my original scenario, the variable "obj" contain lots of entries. And will be called frequently. So looping using $.each will cause performance issue.

If there is another way to declare the above variable, then i can do that also. So if anyone have any other method to get value by key WITHOUT looping, then can you please share.

Thanks in advance.

like image 639
abyin007 Avatar asked Mar 23 '26 12:03

abyin007


1 Answers

You can use

var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
console.log(obj.one); // 1
console.log(obj['two']); // 2

DEMO.

like image 109
The Alpha Avatar answered Mar 26 '26 00:03

The Alpha