I want to return each element of an array object to the console. The code below does return the object, but not sequentially as I would like. When I select Daffy Duck and Bugs Bunny from a view as selected items the array will return "undefinedDaffy DuckBugs Bunny" after the executeQuery fire off.
I'm not sure where the undefined is coming from. I of course want to return Daffy and Bugs individually to the console, however, after the promise resolves I get the following:
undefinedDaffy DuckBugs Bunny
undefined
function callAccepted() {
getSelected().done(function(objects, iLength) {
// Stuff
for (var k = 0; k < iLength; k++) {
console.log(objects[k]);
}
});
} // End callAccepted
// Get user information function
function getSelected() {
var dfd = $.Deferred(function(){
var ctx = SP.ClientContext.get_current();
var clientContext = new SP.ClientContext();
var targetList = clientContext.get_web().get_lists().getByTitle(ListName);
var SelectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
var items = [];
for (var i in SelectedItems) {
var id = SelectedItems[i].id;
var item = targetList.getItemById(id);
clientContext.load(item, "Title");
items.push(item);
}
clientContext.executeQueryAsync(
function(){
for (var j = 0; j < items.length; j++) {
var arrItems = [];
var itemObjects;
var itemLength = items.length;
itemObjects += items[j].get_item("Title");
arrItems.push(itemObjects);
}
dfd.resolve(arrItems, itemLength);
},
function(){
dfd.reject(args.get_message());
}
);
}); // End dfd
return dfd.promise();
} // End getSelected