0

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}]; 
1
  • That is an array of objects, not JSON. JSON is a text-based data-interchange format. It may have been derived from a string of JSON, but in its current form it is not JSON. Commented Feb 23, 2018 at 6:03

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How to access the value of MoveCount & Latitude ?
Sorting is one way of finding the highest count, but would be an inefficient way of doing it, particularly if you have a large dataset.
@jones Once the array is sorted, the item with the highest MoveCount is the first item in the array, so you would access it via movers[0]. So, to get the latitude, you would use movers[0].Latitude, etc.
1

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);

Comments

1

You might reduce the movers by always taking the bigger one:

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

Comments

1

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))

Comments

1

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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.