如何从元素行访问特定值

发布于 2025-02-07 07:51:21 字数 698 浏览 1 评论 0 原文

在角材料表中,我们需要制作这样的数据结构:
{personne:ali,applicationx:10,applicationy:20,applicationz:30}


但是就我而言,我有类似的数据:
{“ ali”:[{“ nameOfapp”:“ applicationx”,“ valueofapp”:10},{“ nameofapp”:“ applicationy”,“ valueofapp”:20},{“ nameofapp”:“:” applicationz”,“ valueofapp”:30}]}

在TD标签中:< td Mat-cell *matcelldef =“ let element”>元素| json}}}
显示整个行的结果:


在我的情况下,我想显示特定的值

valueOfApp from ApplicaitonY (20)

in angular material table we need to make structure of data like this :
{personne: Ali, applicationX: 10, applicationY: 20 , ApplicationZ: 30}

but in my case i have data like :
{ "Ali": [ { "nameofApp": "applicationX", "valueOfApp": 10 }, { "nameofApp": "applicationY", "valueOfApp": 20 }, { "nameofApp": "applicationZ", "valueOfApp": 30 } ] }

in td tag : <td mat-cell *matCellDef="let element"> element | json }}

the result showing the entire line :
enter image description here

in my case i want to show specific value its

valueOfApp from ApplicaitonY (20)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2025-02-14 07:51:21

“简单响应”是,

element.ali[1].valueOfApp

但我认为您应该改变数据。

首先,您可以在数据中获取所有不同的“应用程序”

,可以使用Reald和过滤以删除重复元素

 apps=Object.keys(this.data).reduce((a,b)=>
        [...a,...this.data[b].map(a=>a.nameofApp)],[])
        .filter((x,index,arr)=>arr.indexOf(x)==index)

或使用新集合

apps=Object.keys(this.data).reduce((a,b)=>
     [...new Set([...a,...this.data[b].map(a=>a.nameofApp)])],[])

,然后您以方式转换数据

  dataTransform=Object.keys(this.data).map(key=>{
    const obj={personne:key}
    this.apps.forEach(a=>{
      console.log(this.data[key])
      const app=this.data[key].find(x=>x.nameofApp==a)
      obj[a]=app?app.valueOfApp:0
    })
    return obj
  })

,因此您具有具有属性的一系列元素:“ personne”和不同的应用程序名称

请参见a little stackblitz (我定义时创建变量,通常您需要声明,并且在功能中,您获得数据为它们提供了值)

The "easy response" is

element.ali[1].valueOfApp

But I think you should transform your data.

First you get all the differents "applications" based in your data

You can use reduce and filter to remove the duplicate elements

 apps=Object.keys(this.data).reduce((a,b)=>
        [...a,...this.data[b].map(a=>a.nameofApp)],[])
        .filter((x,index,arr)=>arr.indexOf(x)==index)

Or using new Set

apps=Object.keys(this.data).reduce((a,b)=>
     [...new Set([...a,...this.data[b].map(a=>a.nameofApp)])],[])

Then you transform your data in the way

  dataTransform=Object.keys(this.data).map(key=>{
    const obj={personne:key}
    this.apps.forEach(a=>{
      console.log(this.data[key])
      const app=this.data[key].find(x=>x.nameofApp==a)
      obj[a]=app?app.valueOfApp:0
    })
    return obj
  })

So you has an array of elements with the properties: "personne" and the differents appsNames

See a little stackblitz (I create the variables when I defined, in general you need declare and, in the function you get the data give value to them)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文