最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return Maximum value from a JSON Object - Stack Overflow

programmeradmin4浏览0评论

I have an associative array which looks like:

var data = {
    0: {
        'Number_of_Something': 212
    },
    1: {
        'Number_of_Something': 65
    },
    2: {
        'Number_of_Something': 657
    }
}

I need to extract the highest value in the field Number_of_Something, however, because it is a field within an object of an object, it is a little more plicated than just following a similar method to something outlined here.

Looping through the object and storing the value, then replacing it if the next one is larger seems like the easiest and obvious option.

I am simply asking this question in case there is a simpler (smarter) way of achieving this other than the method outlined above?

I have an associative array which looks like:

var data = {
    0: {
        'Number_of_Something': 212
    },
    1: {
        'Number_of_Something': 65
    },
    2: {
        'Number_of_Something': 657
    }
}

I need to extract the highest value in the field Number_of_Something, however, because it is a field within an object of an object, it is a little more plicated than just following a similar method to something outlined here.

Looping through the object and storing the value, then replacing it if the next one is larger seems like the easiest and obvious option.

I am simply asking this question in case there is a simpler (smarter) way of achieving this other than the method outlined above?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 15, 2013 at 11:07 Ben CareyBen Carey 17k20 gold badges93 silver badges180 bronze badges 5
  • 1 Why is data an object and not an array or at least array-like object? – Felix Kling Commented Apr 15, 2013 at 11:09
  • @FelixKling it is a JSON object passed from a PHP script. I force an object for other purposes :) – Ben Carey Commented Apr 15, 2013 at 11:17
  • What is wrong with looping over the object members? That seems pretty simple, maintainable and very patible to me. – RobG Commented Apr 15, 2013 at 11:22
  • @RobG Absolutely nothing wrong with it, but I just wanted to check there were not any javascript functions already there that would make the process simpler :-) – Ben Carey Commented Apr 15, 2013 at 11:23
  • I don't see how nesting 3 functions is simpler that basic loop. A little less code perhaps, but certainly not simpler. Performance seems mixed, it doesn't seem any faster on balance. – RobG Commented Apr 15, 2013 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 17

simpler can be subjective... Another way to achieve what you ask is to get an array of the values using Object.keys and Array.prototype.map, and use the other solution with Math.max that you linked :

var data = {
    0: {
        'Number_of_Something': 212
    },
    1: {
        'Number_of_Something': 65
    },
    2: {
        'Number_of_Something': 657
    }
}

var max = Math.max.apply(null,
                        Object.keys(data).map(function(e) {
                                return data[e]['Number_of_Something'];
                        }));

You can create your own (for) loop and return the max value that way. Another option is to use a library like Underscore.js to handle this for you and keep your code readable.

发布评论

评论列表(0)

  1. 暂无评论