Not too sure if I’m doing this right. It looks a bit more convoluted than it needs to be. But I am trying to pull a specific piece of data out of a json array. I need several features of this data piece, and then I need to take the return, label the return, and pass it through a method at the bottom of success call.
$(document).ready(function() {
if ( window.location.href.match(/customer_number/).length > 0) {
$customer_id = window.location.href.split(/customer_number=/)[1];
$.ajax({
url: '/customers/filter.json',
data: { id: $customer_id },
dataType: 'json',
success: function(data) {
response($.map(data, function(row) {
return {
value: row.customer.name,
customer_number: row.customer.customer_number,
id: row.customer.id,
name: row.customer.name,
phone_number: row.customer.phone_number,
email: row.customer.email,
service_address: row.customer.service_address,
service_city: row.customer.service_city,
service_state: row.customer.service_state,
service_zip_code: row.customer.service_zip_code,
billing_adress: row.customer.billing_address,
billing_city: row.customer.billing_city,
billing_state: row.customer.billing_state,
billing_zip_code: row.customer.billing_zip_code,
primary_contact_name: row.customer.primary_contact ? row.customer.primary_contact.first_name + ' ' + row.customer.primary_contact.last_name : ''
};
render_customer(name_of_return);
// ^^ Not sure how to label that return into an object.
}));
}
});
};
});
You’re passing the returned object back to
$.map‘s handling of the return values for the iteration function (it builds up those values in an array), and then handing that array into yourresponsefunction. In JavaScript, when you’re calling a function (or returning a value out of one), you don’t give the arguments / return value names. In the case of arguments, it’s entirely positional. (The function definition may assign them names, but you don’t when you call them.) In the case of a return value, there is only ever one.