Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript create a new array from json results

It is probably a simple solution, but I can not seem to get it working :/ I want to create a new "response" from results collected trough json/api call.

jsonResponse = [
     {"_id":"1","desc":"test desc","title":"Title 1"},
     {"_id":"2","title":"Title 2","desc":"desc 2"}
    ];

I need to create a new array from this that looks like this;

var newResponse = [ 
  { "heading" : "Title 1", "summary" : "test desc"},
  { "heading" : "Title 2", "summary" : "desc 2"}

 ];

Stripping _id and changing "key". How can I go about doing it?

like image 998
Tom Avatar asked Dec 15 '22 02:12

Tom


1 Answers

The Array.prototype.map function works wonders:

var newResponse = jsonResponse.map(function(item){
  return { heading : item.title, summary : item.desc };
});
like image 83
Tibos Avatar answered Jan 02 '23 13:01

Tibos