英文:
Output should return top 5 application name based on count property which present ImageDetails object
问题
data = {
"ImageDetails": [
{
"application": "unknownApp",
"count": 107757
},
{
"application": "app6",
"count": 1900
},
{
"application": "app8",
"count": 0
},
{
"application": "app3",
"count": 100
},
{
"application": "app9",
"count": 0
},
{
"application": "app1",
"count": 0
},
{
"application": "app10",
"count": 0
},
{
"application": "app7",
"count": 0
}
]
}
when I subscribed to the API, I am getting data as above.
Output should return the top 5 application names based on the count property in ImageDetails.
Expected Output: [unknownApp, app6, app3, app8, app9]
英文:
data = {
"ImageDetails": [
{
"application": "unknownApp,
"count": 107757,
},
{
"application": "app6",
"count": 1900,
},
{
"application": "app8",
"count": 0,
},
{
"application": "app3",
"count": 100,
},
{
"application": "app9"
"count": 0,
},
{
"application": "app1"
"count": 0,
},
{
"application": "app10"
"count": 0,
},
{
"application": "app7",
"count": 0,
}
]
}
when i subscribed api i am getting data as above.
Output should return top 5 application name based on count
property ImageDetails.
expected Output : [unknownApp,app6,app3,app8,app9]
答案1
得分: 1
Your data is invalid but I fixed it by adding missing quotes and comma:
您的数据无效,但我通过添加缺少的引号和逗号来修复它。
But if the data from the api is malformed you probably have to ask the data provider:
但是,如果来自 API 的数据格式不正确,您可能需要向数据提供者提问。
structuredClone
is used to deep copy the array so when you modify the copied array the original won't be changed:
structuredClone
用于深复制数组,因此当您修改复制的数组时,原始数组不会被更改。
You can sort
the data based on the count then slice
to get top 5:
您可以根据计数对数据进行sort
,然后使用 slice
获取前5个。
Then map
the data to get only the app names:
然后使用 map
对数据进行映射,以获取仅应用程序名称。
const apps = data.ImageDetails
const sortedApps = structuredClone(apps).sort((a, b) => {
return b.count - a.count
})
const top5Apps = sortedApps.slice(0, 5)
const top5AppNames = top5Apps.map(app => app.application)
console.log(top5AppNames)
<script>
// Data here to show the js code on top
const data = {
"ImageDetails": [{
"application": "unknownApp",
"count": 107757,
},
{
"application": "app6",
"count": 1900,
},
{
"application": "app8",
"count": 0,
},
{
"application": "app3",
"count": 100,
},
{
"application": "app9",
"count": 0,
},
{
"application": "app1",
"count": 0,
},
{
"application": "app10",
"count": 0,
},
{
"application": "app7",
"count": 0,
}
]
}
</script>
英文:
Your data is invalid but I fixed it by adding missing quotes and comma
But if the data from the api is malformed you probably have to ask the data provider
structuredClone
is used to deep copy the array so when you modify the copied array the original won't be changed
You can sort
the data based on the count then slice
to get top 5
Then map
the data to get only the app names
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const apps = data.ImageDetails
const sortedApps = structuredClone(apps).sort((a, b) => {
return b.count - a.count
})
const top5Apps = sortedApps.slice(0, 5)
const top5AppNames = top5Apps.map(app => app.application)
console.log(top5AppNames)
<!-- language: lang-html -->
<script>
// Data here to show the js code on top
const data = {
"ImageDetails": [{
"application": "unknownApp",
"count": 107757,
},
{
"application": "app6",
"count": 1900,
},
{
"application": "app8",
"count": 0,
},
{
"application": "app3",
"count": 100,
},
{
"application": "app9",
"count": 0,
},
{
"application": "app1",
"count": 0,
},
{
"application": "app10",
"count": 0,
},
{
"application": "app7",
"count": 0,
}
]
}
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论