admin管理员组文章数量:1365849
I am creating a Angular 2 application. My server side request returns me a JSON that will look like
[{"CountryId":1,"CountryCode":"IND","CountryName":"India","State":[]},
{"CountryId":2,"CountryCode":"AUS","CountryName":"Australia","State":[]}]
I need to display this in a dropdown. So I need to create a new JSON that will look like
[{"Label":"India","Value":"IND"},
{"Label":"Australia","Value":"AUS"}]
I dont want to write for loop as i will be using this in many places and i want it to be best possible in performance. Please let me know if there is any inbuilt function that can do this kind of JSON restructure.
I am creating a Angular 2 application. My server side request returns me a JSON that will look like
[{"CountryId":1,"CountryCode":"IND","CountryName":"India","State":[]},
{"CountryId":2,"CountryCode":"AUS","CountryName":"Australia","State":[]}]
I need to display this in a dropdown. So I need to create a new JSON that will look like
[{"Label":"India","Value":"IND"},
{"Label":"Australia","Value":"AUS"}]
I dont want to write for loop as i will be using this in many places and i want it to be best possible in performance. Please let me know if there is any inbuilt function that can do this kind of JSON restructure.
Share Improve this question edited Aug 21, 2016 at 7:39 Toby Allen 11.2k12 gold badges79 silver badges131 bronze badges asked Aug 21, 2016 at 3:05 VinodVinod 32.9k35 gold badges99 silver badges119 bronze badges 6- 2 Are you sure you need to transform the JSON? Cannot the UI ponent be configured to use alternate keys for label and value? – Thilo Commented Aug 21, 2016 at 3:10
-
3
A
for
loop is exactly what you need (althoughmap()
is nicer). – SLaks Commented Aug 21, 2016 at 3:11 - @Thilo I am using custom library primeng. This probably does not support for now. – Vinod Commented Aug 21, 2016 at 3:16
- Sometimes you just have to write some code darnit! – Toby Allen Commented Aug 21, 2016 at 7:34
- 1 You don't really want to create new JSON. I assume you're converting the JSON resturned to a javascript array and want to convert the objects in the array to objects with different properties. Using JSON in your title and using the JSON tag makes it seem like you are talking about JSON in particular (JavaScript Object Notation), the string representation of javascript objects. – Jason Goemaat Commented Aug 21, 2016 at 12:16
2 Answers
Reset to default 6Unless you know exactly how many elements there will be in your response array you can't escape from iterating it, although you can choose how:
// A
let result = response.map((elem) => ({
'Label': elem.CountryName,
'Value': elem.CountryCode
}))
// B
let result = []
response.forEach((elem) =>
result.push({
'Label': elem.CountryName,
'Value': elem.CountryCode
})
)
// C
let result = []
for (i in response) {
result.push({
'Label': response[i]['CountryName'],
'Value': response[i]['CountryCode']
})
)
Using ES6 destructuring, the most pact form would be:
response.map(({CountryCode: Label, CountryName: Value}) => ({Label, Value}))
本文标签: javascriptModify JSON with typescriptStack Overflow
版权声明:本文标题:javascript - Modify JSON with typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743750967a2532638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论