Javascript 动态获取任何nasted对象数组的所有对象属性名称
假设我有一个nasted数组对象,我想得到该对象的所有结构Javascript 动态获取任何nasted对象数组的所有对象属性名称,javascript,arrays,object,Javascript,Arrays,Object,假设我有一个nasted数组对象,我想得到该对象的所有结构 // any array // i use this array var person = [{firstName:"John", lastName:"Doe", age:46}, {firstName:"John", lastName:"Doe", k:46}, {firstName:"John", o:"Doe", age:46},
// any array
// i use this array
var person = [{firstName:"John", lastName:"Doe", age:46},
{firstName:"John", lastName:"Doe", k:46},
{firstName:"John", o:"Doe", age:46},
{firstName:"John", lastName:"Doe", ppp:[]}];
//how about if it have one nested
var person2 = [{firstName:"John", lastName:"Doe", age:46},
{firstName:"John", lastName:"Doe", k:46},
{firstName:"John", o:"Doe", age:46},
{firstName:"John", lastName:"Doe", ppp:[{fa:"aa",ta:"asfa"},
{fa:"as"}]}];
//i want to achive like this
//["firstName", "lastName", "age", "k", "o", ppp: [fa,ta]]
//how about if it have many nested
var person3 = [{firstName:"John", lastName:"Doe", age:46},
{firstName:"John", lastName:"Doe", k:46 gg:[{ddd:
[{asar:""magrib}]}]},
{firstName:"John", o:"Doe", age:46 kk:["aaa"]},
{firstName:"John", lastName:"Doe", ppp:[{fa:"aa",ta:"asfa"},
{fa:"as"}]}];
我运行函数来得到atribute,但我可以在第一层做,如果这个数组符合对象,我希望这个函数生成属性名
function get_key (Obj,p){
Obj.forEach(function(d,i){
for ( property in d ) {
var type = typeof(d[property])
if(p.indexOf(property)<0){
if (type!=="object"){
p.push(property)
//console.log('ok')
}else{
//if meet object how can i make this do the same thing?
// so it can dynamicly getattribute name
p[property]= p[property]='jj'
}
}
}
})
return p
}
var p = get_key (person,[])
document.getElementById("demo").innerHTML = p;
console.log(p)
//and if we console it it return
//["firstName", "lastName", "age", "k", "o", ppp: "jj"]
函数get_键(Obj,p){
对象forEach(函数(d,i){
对于(d中的属性){
var type=typeof(d[属性])
如果(p.indexOf(property)完成,则可以
function get_key (Obj,p,id){
var p2
Obj.forEach(function(d,i){
if(typeof(d)!=='object'){
return
}
for ( property in d ) {
var type = typeof(d[property])
if(p.indexOf(property)<0){
if (type!=="object"){
p.push(property)
//console.log('ok')
//p[property]='jj'
}else{
console.log(property,[])
var pe= property
p2 = get_key (d[property],[],property)
p[pe]=p2
}
}
}
})
return p
}
var p = get_key (person,[])
函数get_键(Obj、p、id){
变量p2
对象forEach(函数(d,i){
如果(类型(d)!='object'){
返回
}
对于(d中的属性){
var type=typeof(d[属性])
如果(p.indexOf(property)如果你花些时间修正你问题的拼写、语法和标点符号,你可能会得到一些答案。事实上,这很难阅读。使用Obj作为引用数组的变量的名称是令人困惑的。对不起,我只是找不到编辑按钮