简体   繁体   English

如何从json对象中的max属性获取对象属性

[英]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 . 我需要获取具有最高MoveCount的项目的属性。 I have a function which returns me only the MoveCount . 我有一个仅返回MoveCount But I want to get the value of Latitude and Longitude as well. 但是我也想获得LatitudeLongitude的值。

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. 您可以使用基于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. 上面的代码返回一个按MoveCount降序排列的数组。

Sort the array, use map function to change the outcoming objects and get the first item from the sorted array. 对数组进行排序,使用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);

You can sort as others say , but that would take O(nlogn) complexity. 您可以像其他人说的那样进行排序,但这会带来O(nlogn)复杂性。 Or you can just compare the highest value in O(n) complexity. 或者,您可以比较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)) 

I'd probably use Array.prototype.reduce to traverse the array to find the item with the highest MoveCount . 我可能会使用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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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