javascript - Get the highest value from a JSON Object

I'm trying to get theplayer
with the highestrating
from the following JSON object. I have two teamshome
andaway
and I want to compare all ratings from all players and return the highestrating
value and thename
of the player.
{
"lineup": {
"home": {
"starters": [{
"name": "Andreas",
"statistics": {
"rating": 6.38
}
},
{
"name": "Carlos",
"statistics": {
"rating": 6.87
}
}
]
},
"away": {
"starters": [{
"name": "Felix",
"statistics": {
"rating": 7.20
}
},
{
"name": "Daniel",
"statistics": {
"rating": 4.87
}
}
]
}
}
}
Keep in mind that my JSON includes 30players
with theirratings
and not 4
What I've tried so far.
Attempt #1:
I've tried to get themax
fromhome
andaway
team and then compare these two values and get the highest, for some reason it doesn't return the maximum value on each team. Ex for teamhome
returns theplayer
with therating 6.38
instead the other one.
//Home
$max = max($decode_one['lineup']['home']['starters']);
$finalVal = $max['statistics']['rating'];
//Away
$max1 = max($decode_one['lineup']['away']['starters']);
$finalVal1 = $max1['statistics']['rating'];
Attempt #2:
Here I've added the ratings inside to a new array and then with a loop got the max value from the array. The 2 issues that I have is first the JSON includes 30 players, 15 fromhome
and 15away
for some reason it puts only the 15 players fromhome
and not from both. I think that is because the keys from each team are the same (0-14) and the other issue I want as well to returnname
of the selectedplayer
.
$result = array();
foreach ($decode_one['lineup'] as $homeOrAway) {
foreach ($homeOrAway as $startersOrSubs) {
foreach ($startersOrSubs as $key => $value) {
$result[$key['rating']][] = $value['statistics']['rating'];
}
}
}
foreach ($result as $key => $maxValue) {
echo "{$key}: " . max($maxValue) . "\n";
}
Any ideas?
Thank you
Answer
Solution:
PHP version, usingarray_reduce
to find the maximum element in the combined array of players from both “home” and “away”:
$max = array_reduce(
array_merge(
$decode_one['lineup']['home']['starters'],
$decode_one['lineup']['away']['starters']
),
function($carry, $item) {
if( $carry === NULL) {
return $item;
}
else {
return $carry['statistics']['rating'] > $item['statistics']['rating'] ?
$carry : $item;
}
}
);
Answer
Solution:
// A JS solution
getHighestRating = obj => {
let starters = obj.lineup.home.starters.concat(obj.lineup.away.starters), highestRating = 0;
for(const player of starters){
highestRating = highestRating < player.statistics.rating ? player.statistics.rating : highestRating;
}
return highestRating;
}
getHighestRating(obj);
// Hope this will help
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.