I'm not sure if the title is appropriate for what I am trying to achieve
I am mapping JSON results to an array. Because I need to do this over and over again I would like to put the code into one function.
In the following examples I am repeating myself. I have properties called item.data1, item.data2 and in the second example item.something1, item.something2 ... How can I pass those properties as general arguments to the newly created function in order to use them there and not repeat myself to return those maps? The new function should be useable for the two examples below as well as for other cases where the properties could have different names.
service.getData(function(data) {
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
entry.key = item.data1;
entry.value = item.data2;
return entry;
});
});
service.getSomething(function(data) {
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
entry.key = item.something1;
entry.value = item.something2;
return entry;
});
});