从 JavaScript 中的对象获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17635866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get values from an object in JavaScript
提问by Hari krishnan
I have this object:
我有这个对象:
var data = {"id": 1, "second": "abcd"};
These are values from a form. I am passing this to a function for verification.
这些是来自表单的值。我将它传递给一个函数进行验证。
If the above properties exist we can get their values with data["id"]
and data["second"]
, but sometimes, based on other values, the properties can be different.
如果上述属性存在,我们可以使用data["id"]
and获取它们的值data["second"]
,但有时,基于其他值,属性可能不同。
How can I get values from data
independent of property names?
如何从data
独立于属性名称的值中获取值?
回答by cfs
To access the properties of an object without knowing the names of those properties you can use a for ... in
loop:
要在不知道这些属性的名称的情况下访问对象的属性,您可以使用for ... in
循环:
for(key in data) {
if(data.hasOwnProperty(key)) {
var value = data[key];
//do something with value;
}
}
回答by Erel Segal-Halevi
If you want to do this in a single line, try:
如果您想在一行中执行此操作,请尝试:
Object.keys(a).map(function(key){return a[key]})
回答by trincot
In ES2017 you can use Object.values()
:
在 ES2017 中,您可以使用Object.values()
:
Object.values(data)
At the time of writing support is limited (FireFox and Chrome).All major browsers except IE support this now.
在撰写本文时,支持有限(FireFox 和 Chrome)。现在除 IE 之外的所有主要浏览器都支持此功能。
In ES2015 you can use this:
在 ES2015 中你可以使用这个:
Object.keys(data).map(k => data[k])
回答by user3118220
If you $
is defined then You can iterate
如果你$
被定义,那么你可以迭代
var data={"id" : 1, "second" : "abcd"};
$.each(data, function() {
var key = Object.keys(this)[0];
var value = this[key];
//do something with value;
});
You can access it by following way If you know the values of keys
您可以通过以下方式访问它 如果您知道键的值
data.id
or
或者
data["id"]
回答by Cooshal
I am sorry that your concluding question is not that clear but you are wrong from the very first line. The variable data is an Objectnot an Array
很抱歉,您的结论性问题不是那么清楚,但您从第一行就错了。变量数据是一个对象而不是一个数组
To access the attributes of an object is pretty easy:
访问对象的属性非常简单:
alert(data.second);
But, if this does not completely answer your question, please clarify it and post back.
但是,如果这不能完全回答您的问题,请澄清并回帖。
Thanks !
谢谢 !
回答by cs01
Using lodash_.values(object)
使用lodash_.values(object)
_.values({"id": 1, "second": "abcd"})
[ 1, 'abcd' ]
lodash
includes a whole bunch of other functions to work with arrays, objects, collections, strings, and more that you wishwere built into JavaScript (and actually seem to slowly be making their way into the language).
lodash
包括一大堆其他函数来处理数组、对象、集合、字符串,以及更多你希望内置在 JavaScript 中的函数(实际上似乎正在慢慢地进入语言)。
回答by Puneet
use
利用
console.log(variable)
控制台日志(变量)
and if you using google chrome open Console by using Ctrl+Shift+j
如果您使用 google chrome 打开控制台,请使用 Ctrl+Shift+j
Goto >> Console
转到 >> 控制台