Angular更改JSON键名

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

Angular changing JSON keys names

问题

export var single: {
   value: number;
   name: string;
}
英文:

I am quite new to angular and working with API's... In my project I am using ngCharts and this chart requires that the keys names in JSON are "value" and "name"... what I get on the API I'm using is keys names "count" and "label"... how and whre could I change the keys name in my code? I cant change it on API.

this is how I'm getting the data from API in my service:

  getSingleCustomerPurchasesChart(id:number) {
     return this.httpClient.get<typeof single>(`${environment.apiUrl}stat/prodaja/${id}`)
   }

this is the model of data that is required for ngxChart:

 export var single: {
   value: number;
   name: string;
 }

this is what the JSON of API looks like:

[
    {
        "count": 123,
        "label": "Lorem ipsum",
        "id": "42807"
    },
]

答案1

得分: 3

你可以使用rxjs的map操作符

    getSingleCustomerPurchasesChart(id:number) {
      return this.httpClient.get<typeof single(`${environment.apiUrl}stat/prodaja/${id}`)
       .pipe(map(response => {
           return response.map((data) => {
            return {
             name: data.label,
             value: data.count,
            };
           }) 
       }))
    }
英文:

You can use rxjs map operator:

getSingleCustomerPurchasesChart(id:number) {
  return this.httpClient.get<typeof single(`${environment.apiUrl}stat/prodaja/${id}`)
   .pipe(map(response => {
       return response.map((data) => {
        return {
         name: data.label,
         value: data.count,
        };
       }) 
   }))
}

答案2

得分: 2

你可以使用基本的JavaScript来解决这个问题。以下是一个解决方案,但我需要了解一些关于你的数据和你想要绘制的图表的信息,以确保它能正常工作。此外,这是纯粹的JavaScript,你需要自己输入:

const apiData = [
    {
        "count": 123,
        "label": "Lorem ipsum",
        "id": "42807"
    },
    {
        "count": 124,
        "label": "Lorem ipsum 2",
        "id": "42808"
    },
];

const chartData = apiData.map((e) => { 
    return { 
        name: e.label,
        value: e.count
    }
})

console.log(chartData);

请参考这里以获取有关map方法的更多详细信息。

英文:

You can solve this with basic Javascript I think. Here is a solution below, but I would have to know a little bit about your data and what chart you want to draw to be sure that it's going to work. Also, it's plain old javascript, I leave you the typing :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const apiData = [
    {
        &quot;count&quot;: 123,
        &quot;label&quot;: &quot;Lorem ipsum&quot;,
        &quot;id&quot;: &quot;42807&quot;
    },
    {
        &quot;count&quot;: 124,
        &quot;label&quot;: &quot;Lorem ipsum 2&quot;,
        &quot;id&quot;: &quot;42808&quot;
    },
];

const chartData = apiData.map((e) =&gt; { 
	return { 
  name: e.label,
  value: e.count
  }
})

console.log(chartData);

<!-- end snippet -->

Please have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map for more details about the map method.

huangapple
  • 本文由 发表于 2023年7月13日 14:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676385.html
匿名

发表评论

匿名网友

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

确定