Javascript 从JSON对象获取属性的最大值

Javascript 从JSON对象获取属性的最大值,javascript,json,sorting,Javascript,Json,Sorting,我随身携带以下JSON对象: { "nodeId": 2965, "appId": 208, "navigationType": 0, "displaySeq": 0, "displayText": "Delete Testing", "customerAttribute": "", "saveAsDefault": false, "ussdDisplayText": "Delete Testing", "headerForChild": "", "Nodes": [ {

我随身携带以下JSON对象:

{
"nodeId": 2965,
"appId": 208,
"navigationType": 0,
"displaySeq": 0,
"displayText": "Delete Testing",
"customerAttribute": "",
"saveAsDefault": false,
"ussdDisplayText": "Delete Testing",
"headerForChild": "",
"Nodes": [
    {
        "nodeId": 3081,
        "appId": 208,
        "navigationType": 0,
        "displaySeq": 1,
        "displayText": "New node2967",
        "customerAttribute": "",
        "saveAsDefault": false,
        "ussdDisplayText": "New node2967",
        "headerForChild": "",
        "parentNodeId": 2965,
        "Nodes": [
            {
                "nodeId": 3086,
                "appId": 208,
                "navigationType": 0,
                "displaySeq": 1,
                "displayText": "abcd",
                "customerAttribute": "",
                "saveAsDefault": false,
                "ussdDisplayText": "New node1",
                "headerForChild": "",
                "parentNodeId": 3081,
                "Nodes": [],
                "concatWithHeader": false,
                "nodeCode": "208_3085_1",
                "parentCode": "3080",
                "extResponseType": 0,
                "canAdd": false,
                "canEdit": false,
                "canDelete": false,
                "canView": false,
                "setChileNode": false
            }
        ],
        "concatWithHeader": false,
        "nodeCode": "3080",
        "parentCode": "RN",
        "extResponseType": 0,
        "canAdd": false,
        "canEdit": false,
        "canDelete": false,
        "canView": false,
        "setChileNode": false
    }
],
"concatWithHeader": false,
"nodeCode": "RN",
"parentCode": "ROOT_NODE",
"responseType": 1,
"responseText": "Thank you!",
"dynamicResponseFlag": false,
"extResponseType": 0,
"canAdd": false,
"canEdit": false,
"canDelete": false,
"canView": false,
"setChileNode": false
}
我需要此对象的属性
**nodeId**
的最大值,即
3080

我该怎么做?我不想把它分类。只要得到最大值

我试过这个:

var data = rootNode;
        var maxProp = "nodeId";
        var maxValue = -1;
        for (var prop in data) {
          if (data.hasOwnProperty(prop)) {
            var value = data[prop];
            if (value > maxValue) {
              maxProp = prop;
              maxValue = value;
            }
          }
        }
但这将迭代属性,而不是子级。因此,我只得到第一个值作为最大值。

尝试以下方法:

var nodes = data.Nodes, // data is your json
    maxProp = "nodeId",
    maxVal = 0, maxInd = 0;

for (var i = 0; i < nodes.length; i++) {
    var value = parseInt(nodes[i][maxProp], 10);
    if (value > maxVal) {
        maxVal = value;
        maxInd = i;
    }
}

console.log(nodes[maxInd]) // array with maximal nodeId
var nodes=data.nodes,//数据是您的json
maxProp=“nodeId”,
maxVal=0,maxInd=0;
对于(var i=0;i最大值){
maxVal=值;
maxInd=i;
}
}
log(nodes[maxInd])//具有最大nodeId的数组
试试这个:

var nodes = data.Nodes, // data is your json
    maxProp = "nodeId",
    maxVal = 0, maxInd = 0;

for (var i = 0; i < nodes.length; i++) {
    var value = parseInt(nodes[i][maxProp], 10);
    if (value > maxVal) {
        maxVal = value;
        maxInd = i;
    }
}

console.log(nodes[maxInd]) // array with maximal nodeId
var nodes=data.nodes,//数据是您的json
maxProp=“nodeId”,
maxVal=0,maxInd=0;
对于(var i=0;i最大值){
maxVal=值;
maxInd=i;
}
}
log(nodes[maxInd])//具有最大nodeId的数组

如果您有一个用于确定最大值的值数组,也可以使用
Math.max
。使用ES5的数组
映射
,您可以从节点中提取它。如果您不能支持ES5(IE9+),也可以使用或实现

var maxProp = 'nodeId',
    propValues, maxvalue;

propValues = data.Nodes.map(function(node) {
    return node[maxProp];
});

maxValue = Math.max.apply(null, propValues);

jsiddle:

如果您有一个值数组,那么您也可以使用
Math.max
。使用ES5的数组
映射
,您可以从节点中提取它。如果您不能支持ES5(IE9+),也可以使用或实现

var maxProp = 'nodeId',
    propValues, maxvalue;

propValues = data.Nodes.map(function(node) {
    return node[maxProp];
});

maxValue = Math.max.apply(null, propValues);

jsiddle:

您的代码对于单个级别的节点工作正常,但对于多个级别的节点工作正常。查看更新的JSON对象。您的代码返回3081,而我需要
3086
。我需要在多级层次结构的情况下使用最高的值。您的代码在单级节点上运行良好,但在多级情况下运行良好。查看更新的JSON对象。您的代码返回3081,而我需要
3086
。我需要在多层次结构的情况下的最高值。