1

我有以下 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;
            }
          }
        }

但这会迭代属性,而不是子元素。因此,我只得到第一个值作为最大值。

4

2 回答 2

1

尝试这个:

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
于 2013-01-31T12:56:49.497 回答
0

Math.max如果您有一个应该确定最大值的值数组,您也可以使用。使用 ES5 的 array map,你可以从你的节点中提取它。如果你不支持 ES5 (IE9+),你也可以使用jQueryUnderscore.js实现。

var maxProp = 'nodeId',
    propValues, maxvalue;

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

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

JSFiddle:http: //jsfiddle.net/g2YeW/

于 2013-01-31T14:17:43.337 回答