crazydev
crazydev

Reputation: 575

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}];

Upvotes: 0

Views: 100

Answers (5)

Kusuma
Kusuma

Reputation: 266

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.

Upvotes: 2

Useless Code
Useless Code

Reputation: 12402

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

Upvotes: 1

Prasanna
Prasanna

Reputation: 4636

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

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138257

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

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

Upvotes: 1

Suren Srapyan
Suren Srapyan

Reputation: 68645

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

Upvotes: 1

Related Questions