Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:55:06+00:00 2026-06-17T04:55:06+00:00

I have an array of nested objects containing values like GDP, income, population etc.

  • 0

I have an array of nested objects containing values like GDP, income, population etc. per year per state:

// The "records" array looks like this:
[
    {
        name : "...",
        income : [
            [1995, 1234], // [year, value]
            [1996. 1235],
            [...]
        ],
        GDP : [
            [1995, 1234],
            [1996. 1235],
            [...]
        ],
        population : [
            [1995, 1234],
            [1996. 1235],
            [...]
        ]
    }, {
        name : "...",
        income : [
            [...]
        ],
        GDP : [
            [...]
        ],
        population : [
            [...]
        ]
    }, {
        ...
    }
]

Now, I want to find the minimum and maximum (extent) for each dimension over all states and years.

populationExtents = [659651, 82536680];
gdpExtents        = [14250, 2498800];
incomeExtents     = [..., ...];

How can I do this without having to traverse the whole array multiple times? Currently, I’m doing this for each dimension:

var income = records.map(function(d, i) {
    return d.income;
});

var min = d3.min(income, function(d, i) {
    return d3.min(d, function(e) {
            return e[1]; // returns values of each year
        });
});

var max = d3.max(income, function(d, i) {
    return d3.max(d, function(e) {
            return e[1];
        });
});

But I think this is way too complex, since I should be able to calculate all “local” minimums per dimension and state, and then calculate the global minimum over all states in just on pass (instead of one pass per each dimension).

I tried several levels of d3.map and nested d3.min, but am not able to wrap my head around this structure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:55:07+00:00Added an answer on June 17, 2026 at 4:55 am
    function getMaxMin( prop ) {
        var concat = [].concat,
            arr = concat.apply([], records.map(function(value) {
                return concat.apply([], value[prop]);
            }));
    
        return [ Math.min.apply(Math.min, arr), Math.max.apply(Math.max, arr) ];
    }
    

    Or a little prettier:

    function getMaxMin( prop ) {
        var arr = [];
    
        records.map(function(value) {
            arr = arr.concat.apply(arr, value[prop][1]);
        });
    
        return [ Math.min.apply(Math.min, arr), Math.max.apply(Math.max, arr) ];
    }
    

    EDIT: To exclude year [year, value] and putting almost everything under the same loop:

    function getMaxMin() {
        var arrs = [];
    
        records.map(function(value) {
            arrs[0] = arrs[0].concat(value.income);
            arrs[1] = arrs[1].concat(value.GDP);
            arrs[2] = arrs[2].concat(value.population);
        });
    
        arrs[0] = arrs[0].filter(c);
        arrs[1] = arrs[1].filter(c);
        arrs[2] = arrs[2].filter(c);
    
        function c(value, key) {
            return key % 2;
        }
    
        return [
            [ Math.min.apply(Math.min, arrs[0]), Math.max.apply(Math.max, arrs[0]) ],
            [ Math.min.apply(Math.min, arrs[1]), Math.max.apply(Math.max, arrs[1]) ],
            [ Math.min.apply(Math.min, arrs[2]), Math.max.apply(Math.max, arrs[2]) ]
        ];
    }
    
    var maxMin = getMaxMin();
    
    maxMin === [
        [income-min, income-max],
        [GDP-min, GDP-max],
        [population-min, population-max]
    ]
    

    Demo: http://jsbin.com/ecineg/1/embed?javascript,console

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a hierarchically nested associative array. It looks like this: A = {
I have a nested data structure containing objects and arrays. How can I extract
I have an array containing objects that I need to sort, and remove duplicates
I have an object containing nested objects and so on. The level of nesting
I have a nested array with valid numbers => data:- $validData = array(array(1 =>
I have a generated nested Array which I store some data. How can i
I have a simple structure in mongodb, with nested array. How can I update
I have a nested php array. I have the first select populated already. I
I have javascript object which consists of complex array of structures with nested structures

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.