javascript - Modify JSON with typescript - Stack Overflow

I am creating a Angular 2 application. My server side request returns me a JSON that will look like [{&

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 (although map() 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
 |  Show 1 more ment

2 Answers 2

Reset to default 6

Unless 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}))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743750968a4500888.html

相关推荐

  • javascript - Modify JSON with typescript - Stack Overflow

    I am creating a Angular 2 application. My server side request returns me a JSON that will look like [{&

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信