javascript Javascript获取对象内的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26998044/
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
Javascript get Object inside Object
提问by user3595784
I took a php array with double arguments (that has like ['contact']['en'] or ['contact']['fr'] or ['presentation']['en']) and did a json_encode to have its values in javascript (so I can use it for ajax)
我使用了一个带有双参数的 php 数组(例如 ['contact']['en'] 或 ['contact']['fr'] 或 ['presentation']['en'])并做了一个 json_encode在 javascript 中有它的值(所以我可以将它用于 ajax)
when I do alert(JSON.stringify(myvariable, null, 4)); I get something like :
当我做 alert(JSON.stringify(myvariable, null, 4)); 我得到类似的东西:
{
"contact":{
"fr":"value",
"en":"value2",
"es":"value3"
},
"presentation":{
"fr":"value",
"en":"value2"
},
etc...
}
What I want to do is to check if a value is equal to the first object (ex: contact=contact) then do a loop for that gets all the values when the condition is met. So if contact = contact, then get me the value of fr,en and es in contact. I managed to do the first object with:
我想要做的是检查一个值是否等于第一个对象(例如:contact=contact)然后做一个循环,当条件满足时获取所有值。因此,如果contact = contact,那么请获取联系中的fr、en 和es 的值。我设法用以下方法完成了第一个对象:
for (var k in variable){
if(k == valuechecked)
{
}
}
But when I do inside if(k == valuechecked):
但是当我在 if(k == valuechecked) 里面做的时候:
for (var a in k){
alert(a);
}
It shows numbers from 0 up to I believe the amount of k there is (therefore the first object). I managed to do what I want with php but not with javascript ... How do I get the values of the objects inside the validated object?
它显示了从 0 到我相信 k 的数量(因此是第一个对象)的数字。我设法用 php 但不是用 javascript 做我想做的事......我如何获得已验证对象内的对象的值?
回答by T.J. Crowder
It would be:
这将是:
for (var a in variable[k]){
alert(variable[k][a]);
}
k
and a
are strings (the namesof the properties).
k
和a
是字符串(属性的名称)。
To avoid lots of repeated lookups, you'd probably save the object to a temp:
为避免大量重复查找,您可能会将对象保存到临时:
var entry = variable[k];
for (var a in entry){
alert(entry[a]);
}
var variable = {
"contact":{
"fr":"contactvalue",
"en":"contactvalue2",
"es":"contactvalue3"
},
"presentation":{
"fr":"presentationvalue",
"en":"presentationvalue2"
},
};
var valuechecked = "contact";
for (var k in variable){
if(k == valuechecked)
{
var entry = variable[k];
for (var a in entry){
snippet.log(entry[a]);
}
}
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Instead of for-in
loops, another option (just to throw it out there) is:
而不是for-in
循环,另一种选择(只是把它扔在那里)是:
Object.keys(variable).forEach(function(k) {
if (k == valuechecked) {
var entry = variable[k];
Object.keys(entry).forEach(function(a) {
// Use entry[a] here
});
}
});
var variable = {
"contact":{
"fr":"contactvalue",
"en":"contactvalue2",
"es":"contactvalue3"
},
"presentation":{
"fr":"presentationvalue",
"en":"presentationvalue2"
},
};
var valuechecked = "contact";
Object.keys(variable).forEach(function(k) {
if (k == valuechecked) {
var entry = variable[k];
Object.keys(entry).forEach(function(a) {
snippet.log(entry[a]);
});
}
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
It's just handy because A) It only returns ownproperties, not inherited ones; and B) The iterator functions give you a nice contained scope for temps like entry
. And if you do the same thing in other places, you can use named functions instead.
这很方便,因为 A) 它只返回自己的属性,而不是继承的;和 B) 迭代器函数为您提供了一个很好的包含临时范围的范围,例如entry
. 如果你在其他地方做同样的事情,你可以改用命名函数。
Note: Object.keys
and Array#forEach
are ES5 features present on all modern browsers. They can both be polyfilled on older browsers like IE8 if necessary.
注:Object.keys
与Array#forEach
存在于所有现代浏览器ES5功能。如有必要,它们都可以在 IE8 等旧浏览器上进行 polyfill。
回答by Neil Malgaonkar
var obj = {
"contact":{
"fr":"value",
"en":"value2",
"es":"value3"
},
"presentation":{
"fr":"value",
"en":"value2"
}
}
for(var a in obj) {
if(a== 'contact') {
var b = obj[a];
console.log("b is", b);
for(var c in b) {
console.log("c is", c);
}
}
}
this is one way in which you can solve this
这是您可以解决此问题的一种方法
回答by user3595784
This may be what you're looking for. What you can do is this First set the array as a far (which I'm sure you've done)
这可能就是你要找的。你可以做的是首先将数组设置为远(我确定你已经完成了)
var array = {
"contact":{
"fr":"value",
"en":"value2",
"es":"value3"
},
"presentation":{
"fr":"value",
"en":"value2"
},
etc...
}
Here if contact exists. go through it.
如果存在联系,则在此处。通过它。
for(first in array) {
if (first == "contact") {
// array.contact exists
for(k in array.contact) {
// k being key
var thisValue = array.contact[k];
console.log(thisValue);
}
}
}
回答by Moob
There are several ways of doing this (see T.J. Crowder's answer) but if you know that you're after a specific item in the object you can just ask for it directly rather than looking for a match first:
有几种方法可以做到这一点(请参阅TJ Crowder 的回答),但如果您知道您正在寻找对象中的特定项目,您可以直接询问它,而不是先寻找匹配项:
//an example object:
var myObject = {
"contact":{
"fr":"value",
"en":"value2",
"es":"value3"
},
"presentation":{
"fr":"value",
"en":"value2"
}
};
//get the part we are looking for:
var obj = myObject["contact"];
//iterate over the object and alert each item:
for (var item in obj){
alert(item+" = "+obj[item]);
}