Return object with highest number from array containing multiple objects












1















I was able to figure out how to return the highest number from a associative array with multiple objects. But I need the whole object.



I prepared this example:






var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);





I need the index or the whole object. I need to show the text from the object with the highest number.










share|improve this question




















  • 5





    technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

    – ADyson
    Nov 13 '18 at 12:58













  • Once you have the highest number can't you just filter the array on that value?

    – Darren Sweeney
    Nov 13 '18 at 12:59











  • @DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

    – Scott Sauyet
    Nov 13 '18 at 13:00






  • 1





    @ADyson, thanks for the hint. I changed the title of the question.

    – Black
    Nov 13 '18 at 14:09
















1















I was able to figure out how to return the highest number from a associative array with multiple objects. But I need the whole object.



I prepared this example:






var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);





I need the index or the whole object. I need to show the text from the object with the highest number.










share|improve this question




















  • 5





    technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

    – ADyson
    Nov 13 '18 at 12:58













  • Once you have the highest number can't you just filter the array on that value?

    – Darren Sweeney
    Nov 13 '18 at 12:59











  • @DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

    – Scott Sauyet
    Nov 13 '18 at 13:00






  • 1





    @ADyson, thanks for the hint. I changed the title of the question.

    – Black
    Nov 13 '18 at 14:09














1












1








1








I was able to figure out how to return the highest number from a associative array with multiple objects. But I need the whole object.



I prepared this example:






var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);





I need the index or the whole object. I need to show the text from the object with the highest number.










share|improve this question
















I was able to figure out how to return the highest number from a associative array with multiple objects. But I need the whole object.



I prepared this example:






var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);





I need the index or the whole object. I need to show the text from the object with the highest number.






var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);





var data = [
{ nr: 235, text: "foo" }
,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
return data[e]['nr'];
}));

var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:08







Black

















asked Nov 13 '18 at 12:56









BlackBlack

4,439844105




4,439844105








  • 5





    technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

    – ADyson
    Nov 13 '18 at 12:58













  • Once you have the highest number can't you just filter the array on that value?

    – Darren Sweeney
    Nov 13 '18 at 12:59











  • @DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

    – Scott Sauyet
    Nov 13 '18 at 13:00






  • 1





    @ADyson, thanks for the hint. I changed the title of the question.

    – Black
    Nov 13 '18 at 14:09














  • 5





    technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

    – ADyson
    Nov 13 '18 at 12:58













  • Once you have the highest number can't you just filter the array on that value?

    – Darren Sweeney
    Nov 13 '18 at 12:59











  • @DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

    – Scott Sauyet
    Nov 13 '18 at 13:00






  • 1





    @ADyson, thanks for the hint. I changed the title of the question.

    – Black
    Nov 13 '18 at 14:09








5




5





technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

– ADyson
Nov 13 '18 at 12:58







technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object.

– ADyson
Nov 13 '18 at 12:58















Once you have the highest number can't you just filter the array on that value?

– Darren Sweeney
Nov 13 '18 at 12:59





Once you have the highest number can't you just filter the array on that value?

– Darren Sweeney
Nov 13 '18 at 12:59













@DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

– Scott Sauyet
Nov 13 '18 at 13:00





@DarrenSweeney: you could, but see the answer from NinaScholz for a better technique.

– Scott Sauyet
Nov 13 '18 at 13:00




1




1





@ADyson, thanks for the hint. I changed the title of the question.

– Black
Nov 13 '18 at 14:09





@ADyson, thanks for the hint. I changed the title of the question.

– Black
Nov 13 '18 at 14:09












3 Answers
3






active

oldest

votes


















7














You could reduce the array by selecting the one with a greater value.






var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

console.log(topNr);








share|improve this answer

































    3














    You can "sort" the array by "nr" property in descending order and get first element "[0]"






    var data = [
    { nr: 235, text: "foo" }
    ,{ nr: 351, text: "bar" }
    ];

    // slice added so that original data is not mutated
    var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

    console.log(result)








    share|improve this answer


























    • Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

      – Keith
      Nov 13 '18 at 13:08






    • 1





      Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

      – Scott Sauyet
      Nov 13 '18 at 13:10













    • A good use case you mentioned. Updated.

      – Nitish Narang
      Nov 13 '18 at 13:10











    • @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

      – Nitish Narang
      Nov 13 '18 at 13:12



















    1














    You can also use findIndex() method:






    var data = [
    { nr: 235, text: "foo" }
    ,{ nr: 351, text: "bar" }
    ];

    var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
    return data[e]['nr'];
    }));

    var index = data.findIndex(function(ln) {
    return ln.nr === highestNr;
    });

    console.log("Highest 'nr': " + highestNr);
    console.log("Index at nr "+ highestNr + ": " + index);

    //console.log(data[index]);








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281514%2freturn-object-with-highest-number-from-array-containing-multiple-objects%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      You could reduce the array by selecting the one with a greater value.






      var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
      topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

      console.log(topNr);








      share|improve this answer






























        7














        You could reduce the array by selecting the one with a greater value.






        var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
        topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

        console.log(topNr);








        share|improve this answer




























          7












          7








          7







          You could reduce the array by selecting the one with a greater value.






          var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
          topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

          console.log(topNr);








          share|improve this answer















          You could reduce the array by selecting the one with a greater value.






          var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
          topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

          console.log(topNr);








          var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
          topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

          console.log(topNr);





          var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
          topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

          console.log(topNr);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 13:01

























          answered Nov 13 '18 at 12:59









          Nina ScholzNina Scholz

          178k1491158




          178k1491158

























              3














              You can "sort" the array by "nr" property in descending order and get first element "[0]"






              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)








              share|improve this answer


























              • Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

                – Keith
                Nov 13 '18 at 13:08






              • 1





                Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

                – Scott Sauyet
                Nov 13 '18 at 13:10













              • A good use case you mentioned. Updated.

                – Nitish Narang
                Nov 13 '18 at 13:10











              • @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

                – Nitish Narang
                Nov 13 '18 at 13:12
















              3














              You can "sort" the array by "nr" property in descending order and get first element "[0]"






              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)








              share|improve this answer


























              • Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

                – Keith
                Nov 13 '18 at 13:08






              • 1





                Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

                – Scott Sauyet
                Nov 13 '18 at 13:10













              • A good use case you mentioned. Updated.

                – Nitish Narang
                Nov 13 '18 at 13:10











              • @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

                – Nitish Narang
                Nov 13 '18 at 13:12














              3












              3








              3







              You can "sort" the array by "nr" property in descending order and get first element "[0]"






              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)








              share|improve this answer















              You can "sort" the array by "nr" property in descending order and get first element "[0]"






              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)








              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)





              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              // slice added so that original data is not mutated
              var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

              console.log(result)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '18 at 13:10

























              answered Nov 13 '18 at 12:58









              Nitish NarangNitish Narang

              2,948815




              2,948815













              • Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

                – Keith
                Nov 13 '18 at 13:08






              • 1





                Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

                – Scott Sauyet
                Nov 13 '18 at 13:10













              • A good use case you mentioned. Updated.

                – Nitish Narang
                Nov 13 '18 at 13:10











              • @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

                – Nitish Narang
                Nov 13 '18 at 13:12



















              • Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

                – Keith
                Nov 13 '18 at 13:08






              • 1





                Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

                – Scott Sauyet
                Nov 13 '18 at 13:10













              • A good use case you mentioned. Updated.

                – Nitish Narang
                Nov 13 '18 at 13:10











              • @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

                – Nitish Narang
                Nov 13 '18 at 13:12

















              Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

              – Keith
              Nov 13 '18 at 13:08





              Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do .concat(data).sort( if this is not desired.

              – Keith
              Nov 13 '18 at 13:08




              1




              1





              Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

              – Scott Sauyet
              Nov 13 '18 at 13:10







              Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.

              – Scott Sauyet
              Nov 13 '18 at 13:10















              A good use case you mentioned. Updated.

              – Nitish Narang
              Nov 13 '18 at 13:10





              A good use case you mentioned. Updated.

              – Nitish Narang
              Nov 13 '18 at 13:10













              @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

              – Nitish Narang
              Nov 13 '18 at 13:12





              @ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.

              – Nitish Narang
              Nov 13 '18 at 13:12











              1














              You can also use findIndex() method:






              var data = [
              { nr: 235, text: "foo" }
              ,{ nr: 351, text: "bar" }
              ];

              var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
              return data[e]['nr'];
              }));

              var index = data.findIndex(function(ln) {
              return ln.nr === highestNr;
              });

              console.log("Highest 'nr': " + highestNr);
              console.log("Index at nr "+ highestNr + ": " + index);

              //console.log(data[index]);








              share|improve this answer




























                1














                You can also use findIndex() method:






                var data = [
                { nr: 235, text: "foo" }
                ,{ nr: 351, text: "bar" }
                ];

                var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
                return data[e]['nr'];
                }));

                var index = data.findIndex(function(ln) {
                return ln.nr === highestNr;
                });

                console.log("Highest 'nr': " + highestNr);
                console.log("Index at nr "+ highestNr + ": " + index);

                //console.log(data[index]);








                share|improve this answer


























                  1












                  1








                  1







                  You can also use findIndex() method:






                  var data = [
                  { nr: 235, text: "foo" }
                  ,{ nr: 351, text: "bar" }
                  ];

                  var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
                  return data[e]['nr'];
                  }));

                  var index = data.findIndex(function(ln) {
                  return ln.nr === highestNr;
                  });

                  console.log("Highest 'nr': " + highestNr);
                  console.log("Index at nr "+ highestNr + ": " + index);

                  //console.log(data[index]);








                  share|improve this answer













                  You can also use findIndex() method:






                  var data = [
                  { nr: 235, text: "foo" }
                  ,{ nr: 351, text: "bar" }
                  ];

                  var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
                  return data[e]['nr'];
                  }));

                  var index = data.findIndex(function(ln) {
                  return ln.nr === highestNr;
                  });

                  console.log("Highest 'nr': " + highestNr);
                  console.log("Index at nr "+ highestNr + ": " + index);

                  //console.log(data[index]);








                  var data = [
                  { nr: 235, text: "foo" }
                  ,{ nr: 351, text: "bar" }
                  ];

                  var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
                  return data[e]['nr'];
                  }));

                  var index = data.findIndex(function(ln) {
                  return ln.nr === highestNr;
                  });

                  console.log("Highest 'nr': " + highestNr);
                  console.log("Index at nr "+ highestNr + ": " + index);

                  //console.log(data[index]);





                  var data = [
                  { nr: 235, text: "foo" }
                  ,{ nr: 351, text: "bar" }
                  ];

                  var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
                  return data[e]['nr'];
                  }));

                  var index = data.findIndex(function(ln) {
                  return ln.nr === highestNr;
                  });

                  console.log("Highest 'nr': " + highestNr);
                  console.log("Index at nr "+ highestNr + ": " + index);

                  //console.log(data[index]);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 13:03









                  Roland RuulRoland Ruul

                  827414




                  827414






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281514%2freturn-object-with-highest-number-from-array-containing-multiple-objects%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python