下面的JSON数据具有EmployeeNumber和TBD和Number,我只需要从JSON进行TBD计数并附加到父

var myJson={
"Name": "Sam",
"EmployeeNumber": "002",
 "IsManager":"True",
 "children": [{
        "Name": "Ravi",
       "EmployeeNumber": "0008",
         "IsManager":"True",
        "children": [{
                "Name": "Krishna",
                "EmployeeNumber": "TBD"
            }, {
                "Name": "diwakar",
                "EmployeeNumber": "007"
            },
            {
                "Name": "Dillep",
                "EmployeeNumber": "009"
            },
            {
                "Name": "David",
                "EmployeeNumber": "009"
            },
            {
                "Name": "Nicholes",
                "EmployeeNumber": "TBD"
            },
            {
                "Name": "Hari",
                "EmployeeNumber": "TBD"
            }
        ]
    },
    {
        "Name": "Rahul",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "Kiran",
        "EmployeeNumber": "TBD"
    }, {
        "Name": "Kumar",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "AJAy",
        "EmployeeNumber": "TBD"
    }
]


}

从上面的JSON中,我想获取TBD的总数,并将其添加到父名称中:Sam = 4TBD / 0FTE和子TBD Count到Ravi = 3TBD / 3FTE,如果IS Manager为True

预期产量:

var myJson={
"Name": "Sam",
"EmployeeNumber": "002",
"IsManager": "Ture",
"Count": "4TBD/0FTE",
"children": [{
        "Name": "Ravi",
        "EmployeeNumber": "TBD",
        "IsManager": "Ture",
        "Count": "3TBD/3FTE",
        "children": [{
                "Name": "Krishna",
                "EmployeeNumber": "TBD"
            }, {
                "Name": "diwakar",
                "EmployeeNumber": "007"
            },
            {
                "Name": "Dillep",
                "EmployeeNumber": "009"
            },
            {
                "Name": "David",
                "EmployeeNumber": "009"
            },
            {
                "Name": "Nicholes",
                "EmployeeNumber": "TBD"
            },
            {
                "Name": "Hari",
                "EmployeeNumber": "TBD"
            }
        ]
    },
    {
        "Name": "Rahul",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "Kiran",
        "EmployeeNumber": "TBD"
    }, {
        "Name": "Kumar",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "AJAy",
        "EmployeeNumber": "TBD"
    }
]


}

我尝试了以下

    var annCtr=[];
    var annFtr=[];
    arr.forEach(function(myJSON) {
    var ctr=[];
    var ftr=[];
     if (myJSON.EmployeeNumber == "TBD") {
        annCtr.push(myJSON.EmployeeNumber);
        ctr.push(myJSON.EmployeeNumber);
       }
    else
    {
    annFtr.push(myJSON.EmployeeNumber);
    ftr.push(myJSON.EmployeeNumber);
    //console.log("FTR :"+myJSON.EmployeeNumber+" Name "+myJSON.Name);
    }
//console.log("Length CTRCT for each loop: "+ctr.length);
//console.log("Length FTE for each loop: "+ftr.length);
})
function children(myJSON) {
  return myJSON.children;
  console.log(d.children);
}
console.log("Total Length FTE for : "+annFtr.length);
console.log("Total Length CTRCT for: "+annCtr.length);
console.log('//////////////////////////////////////////');


在此先感谢您提供的所有帮助

最佳答案

希望下面的代码片段对您有所帮助。



var myJson={
"Name": "Sam",
"EmployeeNumber": "002",
 "IsManager":"True",
 "children": [{
        "Name": "Ravi",
       "EmployeeNumber": "0008",
         "IsManager":"True",
        "children": [{
                "Name": "Krishna",
                "EmployeeNumber": "TBD"
            }, {
                "Name": "diwakar",
                "EmployeeNumber": "007"
            },
            {
                "Name": "Dillep",
                "EmployeeNumber": "009"
            },
            {
                "Name": "David",
                "EmployeeNumber": "009"
            },
            {
                "Name": "Nicholes",
                "EmployeeNumber": "TBD"
            },
            {
                "Name": "Hari",
                "EmployeeNumber": "TBD"
            }
        ]
    },
    {
        "Name": "Rahul",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "Kiran",
        "EmployeeNumber": "TBD"
    }, {
        "Name": "Kumar",
        "EmployeeNumber": "TBD"
    },
    {
        "Name": "AJAy",
        "EmployeeNumber": "TBD"
    }
]
}

function getReporteeCount(employee) {
employee.count = employee.children.reduce( (result,child) => {
if(child.IsManager)
  getReporteeCount(child);
if (child.EmployeeNumber === 'TBD')
result.TBDCount = result.TBDCount + 1;
else
result.FTECount = result.FTECount + 1;

return result}, {FTECount:0 , TBDCount:0})
}

getReporteeCount(myJson)

console.log(myJson)

关于javascript - JavaScript/Node.js中Json数据中父项下的TBD总计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52262050/

10-11 12:32