我有一個像下面這樣的物件陣列:
const arr_obj = [
{
id: '1',
children: [],
type: 'TYPE1',
},
{
id: '2',
children: [
{
id: '1',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '2',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '3',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
},
{
id: '3',
children: [
{
id: '4',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '5',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '6',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
}
]
我必須找出型別的計數:' MAIN'。這些“ MAIN”將在型別內:“ type2”
所以預期的計數是 6。外部子陣列可以是空的,有時內部子陣列的型別為:“ type2” 根本不存在,如下所示:
children: [] //empty array
children: [
{
id: '1',
children: [],
type: 'TYPE1',
},
] //no children with type: 'TYPE2'
有人可以幫我解決這個問題嗎?編程新手。
編輯:
我試過什么?
const findAllChildrenOfType = (obj, type) => {
let count = 0;
if (obj.type === type) count ;
if (obj.children) {
obj.children.forEach(child => {
const childCount = findAllChildrenOfType(child,
"MAIN");
count = childCount;
})
}
return count;
}
findAllChildrenOfType(arr_obj, "TYPE2");
但這使我始終計數為 0。
uj5u.com熱心網友回復:
const arr_obj = [
{
id: '1',
children: [],
type: 'TYPE1',
},
{
id: '2',
children: [
{
id: '1',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '2',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '3',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
],
type: 'TYPE2',
},
{
id: '3',
children: [
{
id: '4',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '5',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '6',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
],
type: 'TYPE2',
}
];
let count = 0
const findAllChildrenOfType = (obj, type) => {
if (obj?.type === type) count ;
if(obj?.children?.length) {
obj.children.forEach((child) => {
findAllChildrenOfType(child, type);
});
}
// return count;
};
arr_obj.forEach((child) => {
if(child.type === 'TYPE2') {
findAllChildrenOfType(child, "MAIN");
}
});
console.log(count);
uj5u.com熱心網友回復:
您可以在您的findAllChildrenOfType()函式中創建一個小函式,該函式將遍歷陣列,并在遇到具有 given 的物件時增加計數type。然后,您可以count從父函式中回傳,方法是減去帶有初始值type(在本例中為“TYPE2”)的物件數,因為當您增加if(obj.type === type).
const arr_obj=[{id:"1",children:[],type:"TYPE1"},{id:"2",children:[{id:"1",children:[{}],type:"MAIN"},{id:"2",children:[{}],type:"MAIN"},{id:"3",children:[{}],type:"MAIN"}],type:"TYPE2"},{id:"3",children:[{id:"4",children:[{}],type:"MAIN"},{id:"5",children:[{}],type:"MAIN"},{id:"6",children:[{}],type:"MAIN"}],type:"TYPE2"}];
const findAllChildrenOfType = (arr_obj, type) => {
let count = 0;
let findElem = (arr_obj, type) => {
arr_obj.forEach(obj => {
if (obj.type === type) {
count ;
if (obj.children.length) {
return findElem(obj.children, "MAIN");
}
}
});
return count;
}
count = findElem(arr_obj, type);
let fil = arr_obj.filter(obj => obj.type === type).length;
return count - fil;
}
console.log(findAllChildrenOfType(arr_obj, "TYPE2"));
uj5u.com熱心網友回復:
一個小的遞回回圈。
const data=[{id:"1",children:[],type:"TYPE1"},{id:"2",children:[{id:"1",children:[{}],type:"MAIN"},{id:"2",children:[{}],type:"MAIN"},{id:"3",children:[{}],type:"MAIN"}],type:"TYPE2"},{id:"3",children:[{id:"4",children:[{}],type:"MAIN"},{id:"5",children:[{}],type:"MAIN"},{id:"6",children:[{}],type:"MAIN"}],type:"TYPE2"}];
const data2=[{id:"1",children:[],type:"TYPE1"},{id:"2",children:[{id:"1",children:[{}],type:"MAIN"},{id:"2",children:[{}],type:"MAIN"},{id:"3",children:[{}],type:"MAIN"}],type:"TYPE2"},{id:"3",children:[{id:"4",children:[{}],type:"MAIN"},{id:"5",children:[{}],type:"MAIN"},{id:"6",children:[{}],type:"MAIN"},{id:"7",children:[{}],type:"MAIN"},{id:"8",children:[{}],type:"MAIN2"}],type:"TYPE2"}];
function find(arr) {
let count = 0;
function loop(arr) {
for (const obj of arr) {
const { type, children } = obj;
if (type === 'TYPE2') loop(children);
if (type === 'MAIN') count;
}
}
loop(arr);
return count;
}
console.log(find(data));
console.log(find(data2));
uj5u.com熱心網友回復:
而不是將它與我??使用的所有物件一起使用,forEach并為每個物件使用函式,例如:
const arr_obj = [{
id: '1',
children: [],
type: 'TYPE1',
},
{
id: '2',
children: [{
id: '1',
children: [{
//some attributes
}],
type: 'MAIN',
},
{
id: '2',
children: [{
//some attributes
}],
type: 'MAIN',
},
{
id: '3',
children: [{
//some attributes
}],
type: 'MAIN',
},
],
type: 'TYPE2',
},
{
id: '3',
children: [{
id: '4',
children: [{
//some attributes
}],
type: 'MAIN',
},
{
id: '5',
children: [{
//some attributes
}],
type: 'MAIN',
},
{
id: '6',
children: [{
//some attributes
}],
type: 'MAIN',
},
],
type: 'TYPE2'
}
];
const findAllChildrenOfType = (obj, type) => {
let count = 0;
if (obj.type === type) {
if (type === 'MAIN') count ;
if (obj.children) {
obj.children.forEach(child => {
const childCount = findAllChildrenOfType(child,
"MAIN");
count = childCount;
})
}
}
return count;
}
let countAll = 0;
arr_obj.forEach(el => {
countAll = findAllChildrenOfType(el, "TYPE2");
});
console.log(countAll)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/394596.html
標籤:javascript
