Issue
I have object:
data = {
"usa": {
"a": {
"min": 1,
"max": 2,
"avg": 1.5
},
"b": {
"min": 3,
"max": 5,
"avg": 4
}
},
"canada": {
"c": {
"min": 1,
"max": 2,
"avg": 1.5
}
}
}
I would like receive all max values from second dimension, for example:
function getMaxValues(country: string): number[] {
const maxValues: number[] = data[country]???
return maxValues;
}
I any better way than iterate over this object and collect results? In other languages are special functions for this. I don't want use iteration because this object is very large and usually specific functions for this are more efficient.
Solution
You need to get the country
object values
then map
to get max
value.
let data = {
"usa": {
"a": {
"min": 1,
"max": 2,
"avg": 1.5
},
"b": {
"min": 3,
"max": 5,
"avg": 4
}
},
"canada": {
"c": {
"min": 1,
"max": 2,
"avg": 1.5
}
}
}
function getMaxValues(country) {
const maxValues = Object.values(data[country]).map(v => v.max);
return maxValues;
}
console.log(getMaxValues('usa'));
Answered By - Mina
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.