0

i have an array like this,and want to find index of maximum of value .for this sample it should return c1:

      var arr={
       c1:{val: 9, x: 2, y: 0}
       c2:{val: 1, x: 3, y: 0}
       c3:{val: 6, x: 4, y: 0}
        }
  • Your array is invalid. – Muhammet Can TONBUL Feb 22 '18 at 15:11
  • 4
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](https://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. Also see: http://idownvotedbecau.se/noattempt/ – James Hill Feb 22 '18 at 15:12
  • Just some terminology: JavaScript doesn't have "associative arrays." It has objects (with properties with names and values), and it has `Map` instances (with entries with names and values). – T.J. Crowder Feb 22 '18 at 15:12
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Feb 22 '18 at 15:13
  • @MuhammetCanTONBUL:It's not an array, just an object, which is similar to what in some places is called an "asscociative array" – Scott Sauyet Feb 22 '18 at 15:15
  • Is your array `var arr = [ {val: 9, x: 2, y: 0}, {val: 1, x: 3, y: 0}, {val: 6, x: 4, y: 0}, ];` – gurvinder372 Feb 22 '18 at 15:16
  • OP's said `i have an array like ` , i know its not an array. @ScottSauyet – Muhammet Can TONBUL Feb 22 '18 at 15:16
  • Jquery is a dom manipulation library.... not a string manipulation library. not an array manipulation library... – Taplar Feb 22 '18 at 15:19
  • or dup of (if array of objects): https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects – Martin Schneider Feb 22 '18 at 15:25

2 Answers2

2

var arr=[{val: 9, x: 2, y: 0},
         {val: 1, x: 3, y: 0},
         {val: 6, x: 4, y: 0}
        ];
 var max_value = arr.reduce((a,b)=> (a.x+a.y+a.val) > (b.x+b.y+b.val) ? a:b )
   // or if it is the index that you want :
 var max_index = arr.reduce((a,b,i,_)=> (_[a].x+_[a].y+_[a].val) > (b.x+b.y+b.val) ? a:i, 0);

 console.log(max_value);
 console.log(max_index);
nullqube
  • 2,959
  • 19
  • 18
0

Assuming your array is

var arr =  [ 
       {val: 9, x: 2, y: 0}, {val: 1, x: 3, y: 0}, {val: 6, x: 4, y: 0},
];

You can get the max value by using Math.max.apply and map

var output = Math.max.apply( null, arr.map( c => c.val ) )

Or if it is an object (as per your latest update)

var arr = {
   c1:{val: 9, x: 2, y: 0},
   c2:{val: 1, x: 3, y: 0},
   c3:{val: 6, x: 4, y: 0}
};
var maxValue = Math.max.apply( null, Object.values( arr ).map( c => c.val ) )

You can get the index-Of the maxValue by doing

var output = Object.keys(arr).findIndex( s => arr[s].val == maxValue ); 
gurvinder372
  • 66,980
  • 10
  • 72
  • 94