0

This is my data

var data = [{
    "name": "abc",
    "cap": 12
}, {
    "name": "pqr",
    "cap": 10
}, {
    "name": "xyz",
    "cap": 50
}

Now here I want to get max value of "cap" i.e 50. Is there any function to do so. Something like max(data['cap']) ?

Thanks

Domnick

scagood
  • 784
  • 4
  • 11
Domnick
  • 509
  • 8
  • 25
  • There is a way, can you show your try? – Learn on hard way Mar 05 '18 at 10:58
  • `Math.prototype.max( null, data.map( s => s.cap ) )` – gurvinder372 Mar 05 '18 at 10:59
  • @Learnonhardway That is a little bit of a foolish comment, lol. – Red Mar 05 '18 at 10:59
  • Have you tried anything? – Rajesh Mar 05 '18 at 11:00
  • If you're after the largest 'cap' only, you could use 'Math.max' if you're after the object with the largest 'cap' you could use 'sort' to order the array by 'cap' or 'reduce' to purely get the largest object. What are you after? – scagood Mar 05 '18 at 11:03
  • @gurvinder372 Math.max(...data.map( s => s.cap )) – Andrew Bone Mar 05 '18 at 11:03
  • 1
    @AndrewBone yep, that's better as it's less verbose. – gurvinder372 Mar 05 '18 at 11:04
  • Reduce example: `data.reduce((m, n) => m.cap > n.cap ? m : n, data[0])`. Sort example: `data.slice(0).sort((a, b) => b.cap - a.cap)[0]`. If you're after ONLY the max cap then either @gurvinder372 or @Andrew_Bone have nice concise examples. – scagood Mar 05 '18 at 11:07
  • Actually I m using data to generate a chart using plotly. and was trying to make the range dynamic, that's the reason I was trying to first calculate the max to select it as an upper range in the axes – Domnick Mar 05 '18 at 11:07

0 Answers0