I have data returned from an API coming back in a response: response.data

    "comment":"random text",

I need to take only the amount and create a new set of objects like this: [{amount: "45342"}, {amount: "54366"}]

The obvious way would be to push it through a loop like so:

var newArray = [];
var newObject;
var i, len;
for (len = response.data.length, i=0; i<len; ++i){
  newObject = response.data[i];
  newArray.push({amount: newObject.amount});

Is there a better way? I am running on angular 1.5 and jquery

  • 1
    \$\begingroup\$ You can use array.map \$\endgroup\$
    – Hamms
    Feb 11, 2017 at 0:08

1 Answer 1


you can use the map function provided for all arrays. You would need to make sure that response.data always returns an array, even in the case where nothing was returned from the api otherwise you can get an error.

var newArray = response.data.map(function(data) { 
  return {amount: data.amount}

would get you what you are looking for.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.