I have an object with several sub-objects and I would like to retrieve all elements. When running the following code, I only retrieve part of the elements till the 'age'
我有一个带有几个子对象的对象,我想检索所有元素。运行以下代码时,我只检索部分元素,直到'age'
var output = '';
var main_table = {
animal: 'dog',
color:'black',
age: {
year:2016,
month:11,
day:1
},
race:'sheepdog',
parents: {
father:'Dad',
mother:'Mom'
}
};
function test(main_table){
table=main_table;
for (var name in table) {
if (table.hasOwnProperty(name)) {
if (table[name]=="[object Object]") {
test(table[name]);
}
else {
output+=(name+' : '+table[name]+' ');
}
}
}
alert (output);
}
test(main_table)
Some help on it will be highly appreciated.
对它的一些帮助将受到高度赞赏。
3 个解决方案
#1
2
You had created an implicit global variable with this line:
您已使用以下行创建了隐式全局变量:
table=main_table;
by missing out the var
.
错过了var。
I have also refactored a little bit to return the output
at each recursive stage, and alert
at the end.
我还重构了一点,以便在每个递归阶段返回输出,并在结束时发出警报。
var main_table = {
animal: 'dog',
color:'black',
age:
{
year:2016,
month:11,
day:1
},
race:'sheepdog',
parents:
{
father:'Dad',
mother:'Mom'}
};
function test(main_table){
var table=main_table;
var output = '';
for (var name in table)
{
if (table.hasOwnProperty(name))
{
console.log(name, typeof table[name])
if (typeof table[name]== "object")
{
output+=test(table[name]);
}
else
{
output+=(name+' : '+table[name]+' ');
}
}
}
return output;
}
alert(test(main_table))
#2
0
I suggest to use an iterative, over the keys and recursive, over the children, approach, with a proper check
我建议使用迭代,通过键和递归,对孩子,接近,进行适当的检查
if (object[key] !== null && typeof object[key] === 'object') { //...
for iterable objects.
对于可迭代对象。
Methods used:
-
Object.keys
returns an array with own properties of the objectObject.keys返回一个具有该对象自身属性的数组
-
Array#forEach
for iterating the arrayArray#forEach用于迭代数组
function getElements(object) {
var result = [];
Object.keys(object).forEach(function (key) {
if (object[key] !== null && typeof object[key] === 'object') {
result = result.concat(getElements(object[key]));
return;
}
result.push([key, object[key]]);
});
return result;
}
var main_table = { animal: 'dog', color: 'black', age: { year: 2016, month: 11, day: 1 }, race: 'sheepdog', parents: { father: 'Dad', mother: 'Mom' } };
console.log(getElements(main_table));
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
0
Hi you set a wrong scope to your function because of this line table=main_table;
嗨,你为你的函数设置了一个错误的范围,因为这个行table = main_table;
this code will work i suppose :
我想这个代码可行:
var output = '';
var main_table = {
animal: 'dog',
color:'black',
age:
{year:2016,month:11,day:1},
race:'sheepdog',
parents:
{father:'Dad',
mother:'Mom'}
};
function test(table){
for (var name in table)
{
if (table.hasOwnProperty(name))
{
if (table[name]=="[object Object]")
{
test(table[name]);
}
else
{
output+=(name+' : '+table[name]+' ');
}
}
}
alert(output);
}
test(main_table);
#1
2
You had created an implicit global variable with this line:
您已使用以下行创建了隐式全局变量:
table=main_table;
by missing out the var
.
错过了var。
I have also refactored a little bit to return the output
at each recursive stage, and alert
at the end.
我还重构了一点,以便在每个递归阶段返回输出,并在结束时发出警报。
var main_table = {
animal: 'dog',
color:'black',
age:
{
year:2016,
month:11,
day:1
},
race:'sheepdog',
parents:
{
father:'Dad',
mother:'Mom'}
};
function test(main_table){
var table=main_table;
var output = '';
for (var name in table)
{
if (table.hasOwnProperty(name))
{
console.log(name, typeof table[name])
if (typeof table[name]== "object")
{
output+=test(table[name]);
}
else
{
output+=(name+' : '+table[name]+' ');
}
}
}
return output;
}
alert(test(main_table))
#2
0
I suggest to use an iterative, over the keys and recursive, over the children, approach, with a proper check
我建议使用迭代,通过键和递归,对孩子,接近,进行适当的检查
if (object[key] !== null && typeof object[key] === 'object') { //...
for iterable objects.
对于可迭代对象。
Methods used:
-
Object.keys
returns an array with own properties of the objectObject.keys返回一个具有该对象自身属性的数组
-
Array#forEach
for iterating the arrayArray#forEach用于迭代数组
function getElements(object) {
var result = [];
Object.keys(object).forEach(function (key) {
if (object[key] !== null && typeof object[key] === 'object') {
result = result.concat(getElements(object[key]));
return;
}
result.push([key, object[key]]);
});
return result;
}
var main_table = { animal: 'dog', color: 'black', age: { year: 2016, month: 11, day: 1 }, race: 'sheepdog', parents: { father: 'Dad', mother: 'Mom' } };
console.log(getElements(main_table));
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
0
Hi you set a wrong scope to your function because of this line table=main_table;
嗨,你为你的函数设置了一个错误的范围,因为这个行table = main_table;
this code will work i suppose :
我想这个代码可行:
var output = '';
var main_table = {
animal: 'dog',
color:'black',
age:
{year:2016,month:11,day:1},
race:'sheepdog',
parents:
{father:'Dad',
mother:'Mom'}
};
function test(table){
for (var name in table)
{
if (table.hasOwnProperty(name))
{
if (table[name]=="[object Object]")
{
test(table[name]);
}
else
{
output+=(name+' : '+table[name]+' ');
}
}
}
alert(output);
}
test(main_table);