javascript - jquery通过键获取值而不循环

标签 javascript jquery

我在 jquery 中有一个变量如下:

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

有没有一种方法可以在不迭代的情况下通过键获取值。

在我原来的场景中,变量“obj”包含很多条目。并且会经常被调用。所以循环使用 $.each 会导致性能问题。

如果有另一种方法来声明上述变量,那么我也可以这样做。因此,如果有人有任何其他方法可以在不循环的情况下通过键获取值,那么您能分享一下吗。

提前致谢。

最佳答案

你可以使用

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

DEMO.

关于javascript - jquery通过键获取值而不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18974071/

相关文章:

javascript - Zombie.js错误: Timeout: did not get to load all resources on this page

javascript - 我们如何为另一个页面发送隐藏在 php 中的 jquery 金额字段以检索金额值?

jquery - Webkit CSS 绝对定位错误?

java - 让 JQuery 作用于 GWT 动态添加的类名

javascript - React + TypeScript : 'React' refers to a UMD global but the current file is a module. 考虑改为添加导入。 (理由)

javascript - 浏览隐藏在网页上的链接(可访问性问题)

javascript - mCustomScrollbar 在 wrapper 上滚动但不在 ul 上滚动

javascript - 在 ASP.net MVC 5 应用程序中单击按钮时使用 JavaScript/jQuery 将用户重定向到页面

javascript - 最大高度过渡不起作用并且工作缓慢

javascript - 为什么 Bootstrap 词缀会跳到顶部