简体   繁体   中英

Modify JSon format returned by an Ajax request

I have a JQuery plugin where I have the following:

transformResult: function(response, originalQuery) {

}

Inside this function I need to convert the originalQuery Json data:

[
  { "Id": 4, "Title": "Title 4" },
  { "Id": 2, "Title": "Title 2" }
]

Into the response Json data format:

{
  suggestions: [
    { data: "4", value: "Title 4" },
    { data: "2", value: "Title 2" }
  ]
}

Id is the data and Title is the value.

How can I do this?

Thank you, Miguel

var arr =[
  { "Id": 4, "Title": "Title 4" },
  { "Id": 2, "Title": "Title 2" }
]

var obj = {suggestions : []};

$.each(arr, function(i, o) {
    obj.suggestions.push({name : o.Id, value : o.Title});
});

FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM