最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery get value by key WITHOUT looping - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Sep 24, 2013 at 6:03 abyin007abyin007 4012 gold badges5 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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.

Without iteration, then you have to do it directly like

obj.one = 1
obj.two = 2
obj.three = 3
...

You can use the member operator(Dot Notation or Bracket Notation) to do it, there is no need for iteration here

obj.one will give 1 same as obj.two will give 2 Ex:

console.log(obj1.one);
console.log(obj1.two);

or if the key is stored in a different variable like var key = 'one' then obj[key] will give 1

var key = 'three';
console.log(obj1[key]);
发布评论

评论列表(0)

  1. 暂无评论