简体   繁体   中英

How to get object properties from max value property in a json object

I have the following array of objects. I need to obtain the properties of the item which has the highest MoveCount . I have a function which returns me only the MoveCount . But I want to get the value of Latitude and Longitude as well.

The function I used is as follows:

var res = Math.max.apply(Math,movers.map(function(o){
return o.MoveCount, o.Latitude, o.Longitude;}))

alert('Max y = ' + res);

Objects:

var movers = [{"MoveCount":3,"DepartureCountryCode":"MG","Latitude":-18.766947,"Longitude":46.869107},
{"MoveCount":2,"DepartureCountryCode":"MU","Latitude":-20.348404,"Longitude":57.552152},
{"MoveCount":4,"DepartureCountryCode":"ZA","Latitude":-30.559482,"Longitude":22.937506}];

You can use a custom sort function based on MoveCount. This would retain the entire object as it is.

 var movers = [ { "MoveCount":3, "DepartureCountryCode":"MG", "Latitude":-18.766947, "Longitude":46.869107 }, { "MoveCount":2, "DepartureCountryCode":"MU", "Latitude":-20.348404, "Longitude":57.552152 }, { "MoveCount":4, "DepartureCountryCode":"ZA", "Latitude":-30.559482, "Longitude":22.937506 } ]; movers.sort(function(a,b){ return b.MoveCount - a.MoveCount; }); var highestMoveCount = movers[0]; // get the item with the highest MoveCount console.log(highestMoveCount); 

The above code returns an array sorted descending by MoveCount.

Sort the array, use map function to change the outcoming objects and get the first item from the sorted array.

 var movers = [ { "MoveCount": 3, "DepartureCountryCode": "MG", "Latitude": -18.766947, "Longitude": 46.869107 }, { "MoveCount": 2, "DepartureCountryCode": "MU", "Latitude": -20.348404, "Longitude": 57.552152 }, { "MoveCount": 4, "DepartureCountryCode": "ZA", "Latitude": -30.559482, "Longitude": 22.937506 }]; const highest = movers.sort((a,b) => b.MoveCount - a.MoveCount) .map(({MoveCount, Latitude, Longitude}) => ({MoveCount, Latitude, Longitude}))[0]; console.log(highest); 

您可以通过始终选择较大的动子来减少动子:

  const biggest = movers.reduce((max, mover) => max.MoveCount > mover.MoveCount ? max : mover);

You can sort as others say , but that would take O(nlogn) complexity. Or you can just compare the highest value in O(n) complexity.

 const movers = [{"MoveCount":3,"DepartureCountryCode":"MG","Latitude":-18.766947,"Longitude":46.869107}, {"MoveCount":2,"DepartureCountryCode":"MU","Latitude":-20.348404,"Longitude":57.552152}, {"MoveCount":4,"DepartureCountryCode":"ZA","Latitude":-30.559482,"Longitude":22.937506}] let highest = movers[0] for (let index=1; index<movers.length; index += 1) { let movObj = movers[index]; if (movObj.MoveCount > highest.MoveCount) { highest = movObj; } } console.log(highest) // if you only want certain fields const certainFieldsRes = ({MoveCount, Latitude, Longitude}) => ({ MoveCount, Latitude, Longitude }) console.log(certainFieldsRes(highest)) 

I'd probably use Array.prototype.reduce to traverse the array to find the item with the highest MoveCount .

 let movers = [ { "MoveCount":3, "DepartureCountryCode":"MG", "Latitude":-18.766947, "Longitude":46.869107 }, { "MoveCount":2, "DepartureCountryCode":"MU", "Latitude":-20.348404, "Longitude":57.552152 }, { "MoveCount":4, "DepartureCountryCode":"ZA", "Latitude":-30.559482, "Longitude":22.937506 } ]; let getHighestMoveCount = function (arr) { let o = arr.reduce(function (accumulator, currentValue, currentIndex) { // if this is the first item, we have nothing to compare against, // just return this item if (currentIndex === 0) { return currentValue; } else { // if this item has a higher MoveCount than the current highest, // return it if (currentValue.MoveCount > accumulator.MoveCount) { return currentValue; } else { // otherwise return the current highest return accumulator; } } }); // return the object that has the highest MoveCount return o; // This returns the entire object, if you wanted a new object with // only the MoveCount Latitude and Longitude, you would do // this instead: // return {MoveCount: o.MoveCount, Latitude: o.Latitude, Longitude: o.Longitude}; }; console.log(getHighestMoveCount(movers)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM