获取javascript中具有子数组的所有elments
我有以下数组获取javascript中具有子数组的所有elments,javascript,arrays,recursion,Javascript,Arrays,Recursion,我有以下数组 { "id": "parent", "code": "parent", "children": [ { "id": "rtsp", "code": "rtsp", "children": [ { "id": "001", "code": "cam30",
{
"id": "parent",
"code": "parent",
"children": [
{
"id": "rtsp",
"code": "rtsp",
"children": [
{
"id": "001",
"code": "cam30",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "002",
"code": "cam31",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "003",
"code": "cam32",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "004",
"code": "cam10",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "005",
"code": "cam11",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "006",
"code": "cam12",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "007",
"code": "cam13",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "008",
"code": "cam14",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "009",
"code": "cam15",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "010",
"code": "cam16",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "011",
"code": "cam17",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "012",
"code": "cam18",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "013",
"code": "cam19",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "014",
"code": "cam9",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "015",
"code": "cam7",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "016",
"code": "cam8",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "017",
"code": "cam5",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
},
{
"id": "018",
"code": "cam6",
"source": "rtsp://192.168.43.29:8554/test",
"sourceFullScreen": "rtsp://192.168.43.29:8554/test"
}
]
},
{
"id": "test",
"code": "test",
"children": [
{
"id": "cam100",
"code": "cam100",
"source": "cam100",
"sourceFullScreen": "cam100"
},
{
"id": "zone-a",
"code": "zone-a",
"children": [
{
"id": "cam100a",
"code": "cam100a",
"source": "cam100a",
"sourceFullScreen": "cam100a"
}
]
},
{
"id": "cam101",
"code": "cam101",
"source": "changed",
"sourceFullScreen": "changed"
},
{
"id": "cam102",
"code": "cam102",
"source": "cam102",
"sourceFullScreen": "cam102"
},
{
"id": "cam103",
"code": "cam103",
"source": "cam102",
"sourceFullScreen": "cam102"
},
{
"id": "cam105",
"code": "cam105",
"source": "cam105",
"sourceFullScreen": "cam105"
}
]
}
]
}
我想检索所有有子元素的元素代码
我尝试了以下方法
jsonfile.readFile(file)
.then(obj => {
json = JSON.parse(JSON.stringify(obj));
elements = getAllParents(json);
console.log(elements);
$('#parent').empty();
$('#parent').append(firstChoice);
$('#parent').append(elements);
})
.catch(error => console.error(error))
.
.
.
function getAllParents(json) {
let childP = "";
function read(json) {
console.log(json);
if (json.children !== undefined) {
for (let i = 0; i < json.children.length; i++) {
const element = json.children[i];
childP += read(element);
console.log(childP)
}
if(childP.includes(json.code) === false) childP += `<option value="` + json.code + `" >` + json.code + `</option>`;
}
return childP;
}
return read(json);
}
jsonfile.readFile(文件)
.然后(obj=>{
json=json.parse(json.stringify(obj));
elements=getAllParents(json);
控制台日志(元素);
$('#parent').empty();
$(“#父项”).append(第一选择);
$('#parent')。追加(元素);
})
.catch(错误=>console.error(错误))
.
.
.
函数getAllParents(json){
让childP=“”;
函数读取(json){
log(json);
if(json.children!==未定义){
for(设i=0;i
问题是我重复了我的元素
我该怎么办,我的错误在哪里?您可以检查子级是否存在,并使用这些子级构建一个新数组 函数getDataWithChildren(对象){ 如果(!object | | typeof object!='object'| |!object.children)返回; const children=object.children.reduce((r,o)=>{ if(o=getDataWithChildren(o))r.push(o); 返回r; }, []); 返回{…对象,子对象}; } var数据={id:“父”,代码:“父”,子项:[{id:“rtsp”,代码:“rtsp”,子项:[{id:“001”,代码:“cam30”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“002”,代码:“cam31”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test“},{id:“003”,代码:“cam32”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“004”,代码:“cam10”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“005”,代码:“cam11”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“006”,代码:“cam12”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“007”,代码:“cam13”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“008”,代码:“cam14”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“009”,代码:“cam15”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“010”,代码:“cam16”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test“},{id:”011,代码:“cam17”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“012”,代码:“cam18”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“013”,代码:“cam19”,来源:rtsp://192.168.43.29:8554/test,Source全屏:"rtsp://192.168.43.29:8554/test},{id:“014”,代码:“cam9”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“015”,代码:“cam7”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“016”,代码:“cam8”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“017”,代码:“cam5”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“018”,代码:“cam6”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test“}]},{id:“test”,代码:“test”,子项:[{id:“cam100”、代码:“cam100”、源代码:“cam100”、源全屏:“cam100”}、{id:“a区”、代码:“a区”、子项:[{id:“cam100a”、代码:“cam100a”、源代码:“cam100a”}、{id:“cam101”、代码:“cam101”、源代码:“cam101”、源代码:“更改”}、{id:“cam102”、代码:“cam102”、源代码:“cam102”,sourceFullScreen:“cam102”},{id:“cam103”,code:“cam103”,source:“cam102”,sourceFullScreen:“cam102”},{id:“cam105”,code:“cam105”,sourceFullScreen:“cam105”}]}, 结果=getDataWithChildren(数据); console.log(结果);
。作为控制台包装{max height:100%!important;top:0;}
您可以检查子级是否存在,并使用这些子级构建新数组
函数getDataWithChildren(对象){
如果(!object | | typeof object!='object'| |!object.children)返回;
const children=object.children.reduce((r,o)=>{
if(o=getDataWithChildren(o))r.push(o);
返回r;
}, []);
返回{…对象,子对象};
}
var数据={id:“父”,代码:“父”,子项:[{id:“rtsp”,代码:“rtsp”,子项:[{id:“001”,代码:“cam30”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“002”,代码:“cam31”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test“},{id:“003”,代码:“cam32”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“004”,代码:“cam10”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“005”,代码:“cam11”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“006”,代码:“cam12”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“007”,代码:“cam13”,来源:rtsp://192.168.43.29:8554/test,sourceFullScreen:rtsp://192.168.43.29:8554/test},{id:“008”,代码:“cam14”,来源:rtsp://192.168.