如何从JSON对象中获取最大值属性的对象属性



我具有以下对象数组。我需要获取具有最高MoveCount的项目的属性。我的功能只能返回MoveCount。但是我也想获得LatitudeLongitude的值。

我使用的功能如下:

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

对象:

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

您可以根据MoveCount使用自定义 sort 函数。这将保留整个对象。

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

上面的代码返回由MoveCount下降的数组。

对数组进行排序,使用 map函数更改截止对象并从排序的数组中获取第一个项目。

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

您可以按照其他人的说法进行排序,但这将需要O(nlogn(的复杂性。或者,您可以比较O(n(复杂性中的最高值。

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

我可能会使用 Array.prototype.reduce遍历数组,以找到最高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));

最新更新