0

I have array and i searching for the biggest age value with the key, but after i found it i need to find the next one by exclude the parent of first one from search. Here is array and function example:

//Data
data = {"people": 
   [{"male": [
     {"name": "Bob" ,"age": "32"}, 
     {"name":"Mike", "age":"31"}
    ]}, 
   {"female": [
     {"name":"Jessica", "age": "24"}, 
     {"name":"Ann", "age": "23"}
   ]}]}

// Function
var age = 0;

data["people"].forEach(function(item) { 
    for (var key in item) {
        if (item.hasOwnProperty(key)) {
            item[key].forEach(function(person) {
                if (person.age > age) {
                    age = person.age;

                }

            });
        }
    }
});

So based on this example i need get 24 from female. Please give me some advices.

JSFIDDLE

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
Samuel K
  • 33
  • 7

4 Answers4

0

In your condition you can check for female only object.

var maxFemaleAge = 0;

data["people"].forEach(function(item) { 
    for (var key in item) {
        if (key==='female' && item.hasOwnProperty(key)) {
            item[key].forEach(function(person) {
                if (person.age > maxFemaleAge) {
                    maxFemaleAge = person.age;
                }
            });
        }
    }
});
console.log(maxFemaleAge);

This will show max female age in console. Fiddle

Vivek
  • 11,938
  • 19
  • 92
  • 127
0

Normally you find max for each key this way:

var max = {}
...//for each key
max[key] = 0
...//for each val
if(val > max[key]){
  max[key] = val
}
...
Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
0

You could reduce it to another array, if I'm understanding what you want to do correctly:

var ages = data["people"].reduce(function(prev, item) {
    var max = 0;
    for (var key in item) {
        if (item.hasOwnProperty(key)) {
            item[key].forEach(function(person) {
                if (person.age > max) {
                    max = person.age;
                }
            });
        }
        prev.push(max);
        return prev;
    }
}, []);

console.log(ages); // 32, 24
Lee
  • 2,993
  • 3
  • 21
  • 27
0

If there are not too many elements inside your object you could also use sort like here: Sorting an array of JavaScript objects At the moment the structure of you object is not optimal for that approach. On the first step you should bring the entries to a iterable structure. For example:

var persons = [
     {"name": "Bob", "age": "32", "sex": "male"},
     {"name": "Jessica", "age": "24", "sex": "female"},
     {...}];

When your data looks like that it is much easier to work with it.

You can sort your persons very easy:

persons.sort(function(a,b){return b.age - a.age})

If you want to do something more complex you could also use the map method:

females = persons.map(function(a){if (a.sex ==="female"){return a}});

When you combine that two techniques you could get very easy the oldest females or males.

That solution is much more the "JavaScript" way. It is not much code, easy to read and uses the build in JS methods.

Community
  • 1
  • 1
apxp
  • 5,240
  • 4
  • 23
  • 43