I have data that is being returned from a remote server. However the problem that I’m running into is sometimes “thing” will be an array of “things” or just a single “thing”
“thing” has a name and value property, along with a couple of others.
The problem I’m having is when just a single “thing” is returned. Right now I have code that looks like this. There has to be a better way.
var array = [];
if (data.results.thing.length > 1) {
var array = $.map(data.results.thing, function (item) {
return {
label: item.name,
value: item.value
}
});
}
else {
array = $.makeArray({
label: data.results.thing.name,
value: data.results.thing.value
});
}
As it turns out, jQuery is smarter than that; $.makeArray will return a true array as is, or make the object into an array as needed: