我有以下对象
acn: "02 0002 0002"
id: "random"
name: "random"
parent_domain: "parent-random"
subDomainData: {random-l3.1: {…}}
sub_domains: ["random-l3.1"]
timestamp: 1549597441
和subDomainData是另一个可能存在或不存在的对象。 如果存在,它可以具有另一个subDomainData,可以具有一个或多个子代,等等。
我的疑问是如何创建一个函数来递归检查它,如果为true,则将其对象推到另一个数组; 预期的最终数组将是这样的:
[
{
acn: "02 0002 0002"
id: "random"
name: "random"
parent_domain: ""
sub_domains: ["random-l3.1"]
timestamp: 1549597441
},
{ // this object would be the result of the parent subDomainData Object.
acn: "02 0002 0002"
id: "random-sub-1"
name: "random-sub-1"
parent_domain: "parent-random"
sub_domains: ["random-l3.3"]
timestamp: 1549597441
}
]
现在我正在采用这种方法,事实证明这是不够的:
formatDomainList = () => {
const { domain } = this.props;
const domainList = Object.values(domain.subDomainData);
domainList.map(item => (
Object.keys(item.subDomainData).length && (
domainList.push(item.subDomainData)
)
//etc
//etc
));
// console.log('domain list', domainList);
}
答案 0 :(得分:0)
类似的事情将为您服务:
//This is your original object
var someObject = {
acn: "02 0002 0002",
id: "random",
name: "random",
parent_domain: "parent-random",
subDomainData: { someObjectWith_subDomainData },
sub_domains: ["random-l3.1"],
timestamp: 1549597441
};
//This will store the list of subDomains in this global variable.
var subDomainList = [];
function getSubDomainList(objectToLoop){
if(objectToLoop.hasOwnProperty('subDomainData')){
var numOfItems = Object.keys(objectToLoop.subDomainData).length;
var keys = Object.keys(objectToLoop.subDomainData);
for(var i = 0; i< numOfItems; i++){
var key = keys[i];
var obj = objectToLoop.subDomainData[key];
subDomainList.push(obj);
getSubDomainList(obj);
}
}
return;
}
getSubDomainList(someObject);
//The subDomainList will have the array of objects.
答案 1 :(得分:0)
我想您的数据格式是错误的,将第一个对象包装在一个属性中给了您所需的输出
var sourceObj = {
"till-11.1": {
_id: "domains/8999693",
_key: "8999693",
_rev: "_YFjlOei--_",
acn: "01 0001 0001",
activated: false,
id: "till",
name: "Till Payments",
timestamp: 1549838701,
sub_domains: ["till-l2.1", "till-l2.2"],
parent_domain: null,
subDomainData: {
"till-l2.1": {
_id: "domains/8999741",
_key: "8999741",
_rev: "_YFgLFKW--_",
acn: "02 0002 0002",
activated: false,
id: "till-l2.1",
name: "Till l2-1",
timestamp: 1549597441,
sub_domains: ["till-l3.1"],
parent_domain: "till",
subDomainData: {
"till-l3.1": {
_id: "domains/9001753",
_key: "9001753",
_rev: "_YFghVqi--_",
acn: "",
activated: true,
id: "till-l3.1",
name: "till l3.1",
timestamp: 1549598900,
sub_domains: [],
parent_domain: "till-l2.1",
subDomainData: {},
accessDomains: []
}
},
accessDomains: ["till-l3.1"]
},
"till-l2.2": {
_id: "domains/8999780",
_key: "8999780",
_rev: "_YFgLcri--_",
acn: "02 0002 0002",
activated: false,
id: "till-l2.2",
name: "Till l2-2",
timestamp: 1549597465,
sub_domains: [],
parent_domain: "till",
subDomainData: {},
accessDomains: []
}
},
accessDomains: ["till-l2.1", "till-l2.2", "till-l3.1"]
}
};
var result = [];
const checkSubDomainData = obj => {
Object.keys(obj).forEach(key => {
if (obj[key].subDomainData !== {}) {
let innerObjs = obj[key].subDomainData;
delete obj[key].subDomainData;
result.push(obj[key]);
checkSubDomainData(innerObjs);
} else {
return;
}
});
};
checkSubDomainData(sourceObj);
console.log(result);