I have a function that converts an object literal response data into an array:
From: [{"_id":"1","rating":7},{"_id":"2","rating":3}]
To: [7,3]
function createArray(fieldVar, responseData) {
var newArray = responseData.map(function(data) {
return data.fieldVar // fails here because I am trying to use the fieldVar variable
});
return newArray
};
I call the function like this:
createArray('rating',response.rating_x)
This fails because of the data.fieldVar
If I hardcode the call to data.rating
it works correctly.
How do I pass in the 'rating' or any other name on the function call?