简体   繁体   English

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

[英]get the max value of a property from a JSON object

I have the following JSON object with me: 我有以下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
}

I want the max value of the property **nodeId** from this object ie 3080 我想要此对象(即3080 **nodeId**属性的最大值

How do I do it? 我该怎么做? I dont want to sort it. 我不想排序。 Just get the max value. 只要获得最大值。

I tried this: 我尝试了这个:

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;
            }
          }
        }

But this iterates over the properties, not on the children. 但这会遍历属性,而不是遍历子级。 Hence I get only the first value as the max one. 因此,我只获得第一个值作为最大值。

Try this: 尝试这个:

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

You could also use Math.max if you have an array of the values from which the max should be determined. 如果您具有应从中确定最大值的值的数组,则也可以使用Math.max Using ES5's array map , you can pull this from your nodes. 使用ES5的数组map ,可以将其从节点中拉出。 If you can't support ES5 (IE9+), you could also use the jQuery or Underscore.js implementations. 如果您不支持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/ JSFiddle: http : //jsfiddle.net/g2YeW/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM