1

I need a function that will search/filter the biggest value in a nested array and then return parent scope.

My array looks next:

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

And I need to find the biggest age value from all people and then return back male or female array (in example this is male array)

With javascript I can use something like:

largest = array.reduce((x, y) ->
      if x > y then x else y
    )
    console.log largest

but how this can be implement to nested array?

Or is there a way to use angular $filter?

1
  • It's not a nested array you're using. it's an object whcih has some properties with their values being an array. Commented Dec 3, 2015 at 15:26

2 Answers 2

0

You can see here for how to search a nested object: Find by key deep in a nested object

Then you can just loop through all the items in your array to search them.

I would suggest a rather different approach, if you can guarantee the structure of your array and those nested objects. First you loop through the array (male/female), and for each of them you loop through the persons and find their age. Its not exact code but something of the likes of:

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;
                }
            });
        }
    }
});
console.log(age);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, works great. Based on this example what is the best way to find next biggest age value (in my data example it will be "age": "24")?
Off the top of my head, after finding the largest age - remove that person from the list and re-run the function. That would probably be the least-code solution, but not very efficient. If you are potentially working with tens of thousand of people entry's, you should just have age1 and age2 variables, and update them both sequentially.
0

It's an ugly data representation but anyway:

var data = {"people": 
   [{"male": [
     {"name": "Bob" ,"age": "32"}, 
     {"name":"Mike", "age":"22"}
    ]}, 
   {"female": [
     {"name":"Jessica", "age": "24"}, 
     {"name":"Ann", "age": "23"}
   ]}]};

function getMaxAge(array) {
  var max = 0;
  array.forEach(function(people) {
    if (people.age > max) max = people.age;
  })
  return max;
}

var max = 0;
var arrayWithMax;
for (var i = 0; i < data.people.length; i++) {
  var array = data.people[i][Object.keys(data.people[i])[0]];
  var currentMax = getMaxAge(array);
  if (currentMax > max) {
    max = currentMax;
    arrayWithMax = array;
  }
}

console.log('max age: ' + max)
console.log(arrayWithMax);

See this code working in https://jsfiddle.net/ave5hcLu/

I'm assuming your data structure is fixed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.