应根据 ImageDetails 对象中的 count 属性返回前 5 个应用程序名称。

huangapple go评论74阅读模式
英文:

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) =&gt; {
  return b.count - a.count
})

const top5Apps = sortedApps.slice(0, 5)

const top5AppNames = top5Apps.map(app =&gt; app.application)

console.log(top5AppNames)

<!-- language: lang-html -->

&lt;script&gt;
  // Data here to show the js code on top
  const data = {
    &quot;ImageDetails&quot;: [{
        &quot;application&quot;: &quot;unknownApp&quot;,
        &quot;count&quot;: 107757,
      },
      {
        &quot;application&quot;: &quot;app6&quot;,
        &quot;count&quot;: 1900,
      },
      {
        &quot;application&quot;: &quot;app8&quot;,
        &quot;count&quot;: 0,
      },
      {
        &quot;application&quot;: &quot;app3&quot;,
        &quot;count&quot;: 100,
      },
      {
        &quot;application&quot;: &quot;app9&quot;,
        &quot;count&quot;: 0,
      },
      {
        &quot;application&quot;: &quot;app1&quot;,
        &quot;count&quot;: 0,
      },
      {
        &quot;application&quot;: &quot;app10&quot;,
        &quot;count&quot;: 0,
      },
      {
        &quot;application&quot;: &quot;app7&quot;,
        &quot;count&quot;: 0,
      }
    ]
  }
&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月10日 12:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定