Количество сотрудников во вложенном json

Я пытаюсь получить количество сотрудников под менеджером и пытаюсь добавить его, если Manager = True, из вложенного ниже JSON

[{
"name": "Wendeline",
"IsManager": "TRUE",
"children": [{
    "name": "Goraud",
    "IsManager": "TRUE",
    "EmployeeNumber": 706997,
    "children": [{
        "name": "Mersey",
        "IsManager": "FALSE",
        "EmployeeNumber": 102752,
        "children": []
    }, {
        "name": "Elli",
        "IsManager": "FALSE",
        "EmployeeNumber": 742047,
        "children": [{
            "name": "Hans",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }]
    }, {
        "name": "Bald",
        "IsManager": "FALSE",
        "EmployeeNumber": 741064,
        "children": []
    }, {
        "name": "Truda",
        "IsManager": "FALSE",
        "EmployeeNumber": 739162,
        "children": []
    }, {
        "name": "Matty",
        "IsManager": "FALSE",
        "EmployeeNumber": 717067,
        "children": []
    }, {
        "name": "Ola",
        "IsManager": "TRUE",
        "EmployeeNumber": 713122,
        "children": [{
            "name": "Frankie",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Quint",
            "IsManager": "FALSE",
            "EmployeeNumber": 705616,
            "children": []
        }, {
            "name": "Georgi",
            "IsManager": "FALSE",
            "EmployeeNumber": 733001,
            "children": []
        }, {
            "name": "Danie",
            "IsManager": "FALSE",
            "EmployeeNumber": 742962,
            "children": []
        }, {
            "name": "Judon",
            "IsManager": "FALSE",
            "EmployeeNumber": 750620,
            "children": []
        }, {
            "name": "Nellie",
            "IsManager": "FALSE",
            "EmployeeNumber": 101490,
            "children": []
        }, {
            "name": "Marji",
            "IsManager": "FALSE",
            "EmployeeNumber": 705660,
            "children": []
        }, {
            "name": "Adelaida",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Bing",
            "IsManager": "FALSE",
            "EmployeeNumber": 745205,
            "children": []
        }, {
            "name": "Odilia",
            "IsManager": "FALSE",
            "EmployeeNumber": 718622,
            "children": []
        }]
    }, {
        "name": "Bernardina",
        "IsManager": "FALSE",
        "EmployeeNumber": 726710,
        "children": []
    }, {
        "name": "Myca",
        "IsManager": "FALSE",
        "EmployeeNumber": 726163,
        "children": []
    }, {
        "name": "Husain",
        "IsManager": "FALSE",
        "EmployeeNumber": 728267,
        "children": []
    }, {
        "name": "Cristie",
        "IsManager": "FALSE",
        "EmployeeNumber": 750267,
        "children": []
    }, {
        "name": "Nikolai",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "James",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Ashbey",
        "IsManager": "FALSE",
        "EmployeeNumber": 744719,
        "children": []
    }, {
        "name": "Flss",
        "IsManager": "FALSE",
        "EmployeeNumber": 746709,
        "children": []
    }, {
        "name": "Felisha",
        "IsManager": "FALSE",
        "EmployeeNumber": 739945,
        "children": [{
            "name": "Zelig",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Jakob",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }]
    }]
}, {
    "name": "Cointon",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Jo",
    "IsManager": "TRUE",
    "EmployeeNumber": 716884,
    "children": [{
        "name": "Sylvia",
        "IsManager": "FALSE",
        "EmployeeNumber": 744160,
        "children": []
    }, {
        "name": "Vicki",
        "IsManager": "FALSE",
        "EmployeeNumber": 735637,
        "children": []
    }, {
        "name": "Blanche",
        "IsManager": "FALSE",
        "EmployeeNumber": 729407,
        "children": []
    }, {
        "name": "Mirabelle",
        "IsManager": "FALSE",
        "EmployeeNumber": 731551,
        "children": []
    }, {
        "name": "Floria",
        "IsManager": "FALSE",
        "EmployeeNumber": 727255,
        "children": []
    }, {
        "name": "Flossie",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Anny",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Hyatt",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Juli",
        "IsManager": "FALSE",
        "EmployeeNumber": 740132,
        "children": []
    }, {
        "name": "Isacco",
        "IsManager": "FALSE",
        "EmployeeNumber": 742246,
        "children": []
    }, {
        "name": "Fleurette",
        "IsManager": "FALSE",
        "EmployeeNumber": 742205,
        "children": []
    }, {
        "name": "Dicky",
        "IsManager": "FALSE",
        "EmployeeNumber": 742736,
        "children": []
    }, {
        "name": "Wini",
        "IsManager": "FALSE",
        "EmployeeNumber": 741836,
        "children": []
    }, {
        "name": "Elden",
        "IsManager": "FALSE",
        "EmployeeNumber": 743234,
        "children": []
    }, {
        "name": "Marjie",
        "IsManager": "FALSE",
        "EmployeeNumber": 740196,
        "children": []
    }, {
        "name": "Vanna",
        "IsManager": "FALSE",
        "EmployeeNumber": 738053,
        "children": []
    }, {
        "name": "Emilio",
        "IsManager": "FALSE",
        "EmployeeNumber": 751901,
        "children": []
    }, {
        "name": "Enrique",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Lilas",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Elita",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Hyacintha",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Markus",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Calhoun",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Melisande",
        "IsManager": "FALSE",
        "EmployeeNumber": 748847,
        "children": []
    }, {
        "name": "Haroun",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Niles",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Adorne",
        "IsManager": "FALSE",
        "EmployeeNumber": 724804,
        "children": []
    }, {
        "name": "Dianne",
        "IsManager": "FALSE",
        "EmployeeNumber": 724863,
        "children": []
    }, {
        "name": "Faye",
        "IsManager": "FALSE",
        "EmployeeNumber": 732329,
        "children": []
    }, {
        "name": "Tabina",
        "IsManager": "FALSE",
        "EmployeeNumber": 724910,
        "children": []
    }]
}, {
    "name": "Spike",
    "IsManager": "FALSE",
    "EmployeeNumber": 732941,
    "children": [{
        "name": "Reta",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Cirillo",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }]
}, {
    "name": "Berty",
    "IsManager": "TRUE",
    "EmployeeNumber": 726527,
    "children": [{
        "name": "Gerrie",
        "IsManager": "TRUE",
        "EmployeeNumber": 727939,
        "children": [{
            "name": "Kelci",
            "IsManager": "FALSE",
            "EmployeeNumber": 739483,
            "children": []
        }, {
            "name": "Alexandros",
            "IsManager": "FALSE",
            "EmployeeNumber": 723369,
            "children": []
        }, {
            "name": "Loreen",
            "IsManager": "FALSE",
            "EmployeeNumber": 746432,
            "children": []
        }, {
            "name": "Cortie",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Keven",
            "IsManager": "FALSE",
            "EmployeeNumber": 723060,
            "children": []
        }, {
            "name": "Shanie",
            "IsManager": "FALSE",
            "EmployeeNumber": 707562,
            "children": []
        }, {
            "name": "Hatti",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Tammi",
            "IsManager": "FALSE",
            "EmployeeNumber": 717763,
            "children": []
        }, {
            "name": "Blondell",
            "IsManager": "FALSE",
            "EmployeeNumber": 731167,
            "children": []
        }]
    }, {
        "name": "Bellina",
        "IsManager": "FALSE",
        "EmployeeNumber": 731162,
        "children": []
    }, {
        "name": "Constancia",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
    }, {
        "name": "Dieter",
        "IsManager": "FALSE",
        "EmployeeNumber": 725942,
        "children": []
    }, {
        "name": "Illa",
        "IsManager": "FALSE",
        "EmployeeNumber": 733953,
        "children": [{
            "name": "Roley",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Talbot",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }]
    }, {
        "name": "Theodora",
        "IsManager": "TRUE",
        "EmployeeNumber": 721734,
        "children": [{
            "name": "Ebeneser",
            "IsManager": "FALSE",
            "EmployeeNumber": 741729,
            "children": []
        }, {
            "name": "Allie",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Benita",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Elmira",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Dene",
            "IsManager": "FALSE",
            "EmployeeNumber": 739572,
            "children": []
        }, {
            "name": "Hamil",
            "IsManager": "FALSE",
            "EmployeeNumber": 731393,
            "children": []
        }, {
            "name": "Fitzgerald",
            "IsManager": "FALSE",
            "EmployeeNumber": 710334,
            "children": [{
                "name": "Leon",
                "IsManager": "FALSE",
                "EmployeeNumber": "TBD",
                "children": []
            }]
        }, {
            "name": "Mignon",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Constantina",
            "IsManager": "FALSE",
            "EmployeeNumber": 744831,
            "children": []
        }, {
            "name": "Alysa",
            "IsManager": "FALSE",
            "EmployeeNumber": 736292,
            "children": []
        }]
    }]
}, {
    "name": "Garrik",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Onofredo",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Josefa",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Nan",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Birgit",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Ashlan",
    "IsManager": "FALSE",
    "EmployeeNumber": "TBD",
    "children": []
}, {
    "name": "Rod",
    "IsManager": "TRUE",
    "EmployeeNumber": 726043,
    "children": [{
        "name": "Lorie",
        "IsManager": "TRUE",
        "EmployeeNumber": 711520,
        "children": [{
            "name": "Norris",
            "IsManager": "FALSE",
            "EmployeeNumber": 736426,
            "children": []
        }, {
            "name": "Josefa",
            "IsManager": "FALSE",
            "EmployeeNumber": 728365,
            "children": []
        }, {
            "name": "Carma",
            "IsManager": "FALSE",
            "EmployeeNumber": 751153,
            "children": []
        }, {
            "name": "Rozelle",
            "IsManager": "FALSE",
            "EmployeeNumber": 738418,
            "children": []
        }, {
            "name": "Tine",
            "IsManager": "FALSE",
            "EmployeeNumber": 721367,
            "children": []
        }, {
            "name": "Ransom",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Hildagarde",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Grant",
            "IsManager": "FALSE",
            "EmployeeNumber": 712948,
            "children": []
        }]
    }, {
        "name": "Karilynn",
        "IsManager": "FALSE",
        "EmployeeNumber": 102152,
        "children": []
    }, {
        "name": "Tine",
        "IsManager": "FALSE",
        "EmployeeNumber": 717285,
        "children": []
    }, {
        "name": "Lenna",
        "IsManager": "FALSE",
        "EmployeeNumber": 707659,
        "children": []
    }, {
        "name": "Fanni",
        "IsManager": "TRUE",
        "EmployeeNumber": 102936,
        "children": [{
            "name": "Nani",
            "IsManager": "FALSE",
            "EmployeeNumber": 726746,
            "children": []
        }, {
            "name": "Jethro",
            "IsManager": "FALSE",
            "EmployeeNumber": 744442,
            "children": []
        }, {
            "name": "Olenka",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Morena",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Susana",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Hammad",
            "IsManager": "FALSE",
            "EmployeeNumber": "TBD",
            "children": []
        }, {
            "name": "Erwin",
            "IsManager": "FALSE",
            "EmployeeNumber": 711550,
            "children": []
        }]
    }, {
        "name": "Petr",
        "IsManager": "FALSE",
        "EmployeeNumber": "TBD",
        "children": []
       }]
   }]
  }]

Я пробовал эту ссылку, которая аналогична моему требованию

Но в моем случае это вложенный JSON Я ищу этот результат, как показано ниже

Если Is Ясли = Правда, добавьте этот счетчик в качестве ключевого значения для родительского элемента.

«Ключ (количество)»: «DirectChildrencount / TBDCount / EmployeeCount (только тот, у кого есть номер сотрудника)»

Пример:

{
"name":"Wendeline",
"IsManager": "TRUE",
"count":"8/4TBD/3FTE"
}

Любая помощь будет оценена заранее

0
0
60
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Мы можем получить DirectChildrencount, используя свойство children.length. Далее мы будем использовать Array.prototype.reduce () для вычисления TBDCount и EmployeeCount. Мы будем использовать Array.prototype.map () для рекурсивного перебора children.

Вот фрагмент кода, который манипулирует вводом для получения требуемого вывода. Предположим, a - это ваш массив:

const a = [ /* */ ]

const transform = x => {
    if (x.IsManager === "TRUE") {
        const child = x.children.reduce(
            (acc, curr) => {
                acc.tbd = curr.EmployeeNumber === "TBD" ? acc.tbd + 1 : acc.tbd;
                acc.fte = curr.EmployeeNumber !== "TBD" ? acc.fte + 1 : acc.fte;
                return acc;
            },
            { tbd: 0, fte: 0 }
        );
        x.count = `${x.children.length}/${child.tbd}TBD/${child.fte}FTE`;
    }
    x.children = x.children.map(transform);
    return x;
};
const b = a.map(transform);
console.log(b);

Поскольку мы назначаем выход map на x.children, вы также можете:

a = [/* */];
a.forEach(transform);
console.log(a)

Я получаю то же, что и входные данные. Поле счетчика не отображается.

RRoy 13.09.2018 21:14

Как бы глупо это ни звучало, поле count могло быть напечатано после массива children, и поэтому вы не сможете его видеть, поскольку массив children слишком велик. Я протестировал его на своей машине, и он работает. Попробуйте это: console.log(b[0].count)

Anand Undavia 13.09.2018 22:03

Другие вопросы по теме