-2

I have an array like this.And I want to know the maximum value of sequence.

                            array = [
                        {


                            "Id" : "123",
                            "Description" : "A Test 1",
                            "isSelected": true,
                            "Sequence" : 1
                        },
                        {
                            "Id" : "124",
                            "Description" : "C Test 2",
                            "isSelected": true,
                            "Sequence" : 2
                        },
                        {
                            "Id" : "125",
                            "Description" : "B Test 3",
                            "isSelected": true,
                            "Sequence" : 3
                        },
                        {
                            "Id" : "126",
                            "Description" : "Z Test 4",
                            "isSelected": true,
                            "Sequence" : 4
                        }
]

Now I want to find out Sequence with maximum value. In this case it will be 4. I need this value to match with the value user enters in the input. I tried Math.max but it was giving me undefined. Thank you for help.

zee
  • 41
  • 1
  • 6
  • and I need solutions in Jquery please. thank you – zee Sep 23 '16 at 19:46
  • Don't diss SO users - Show your code attempts! – The One and Only ChemistryBlob Sep 23 '16 at 19:48
  • 1
    Asking for people to write code for you is not polite. Please try something and then ask a question if it doesn't work. You can write it in one line using `Math.max and Array.map` – Ruan Mendes Sep 23 '16 at 19:49
  • Make an array of all the Sequence values. Then get the maximum from the array, described here: http://stackoverflow.com/questions/14693568/jquery-get-maximum-value-in-an-array-of-numbers – Barmar Sep 23 '16 at 19:51
  • ofcourse I am asking question because it didn't work, and I mentioned that i tried Math.max but gave me undefined, didn't want to copy console outputs, and just asked for help, any hints would be help. – zee Sep 23 '16 at 20:01
  • @zee Then you should have posted what you tried, what you expected to happen, what actually happened together with any error messages so we could tell you what you were doing wrong. And uhhhh, I did show you how to do it in my answer? – Ruan Mendes Sep 23 '16 at 20:12
  • yes I saw that, I was going to try that :) – zee Sep 23 '16 at 20:23

1 Answers1

2

I don't typically just paste code, but any JavaScript beginner can learn something from this snippet that answers your question. No jQuery needed.

Math.max.apply(Math, array.map(o => o.Sequence));

// With the new spread operator, it could be
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
Math.max(...array.map(o => o.Sequence)); 

array.map will create an array of all the sequences. Math.max gives you the maximum value of the arguments passed in, but it's usually called like Math.max(2,5,4,7,4,9) so I use Function.apply to spread the array into separate arguments.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217