Having some difficulty parsing the results of an array and displaying them in the console. There are two issues at play here. First, when building the array, it seems to be adding "undefined" to the results. Second, when attempting to loop through the individual strings in the array, it's not parsing correctly and instead just returning the full array object.
The goal is to gather all the field values selected from a list view and write them to another child list as separate items. However, when the results are displayed in the console, they appear as an object array. Running the typeof method on it seems to indicate that it's being treated as a string.
In summary, why is "undefined" being included and why is the array not being printed to the console correctly? Below is an example of the current output (with two records selected) and the accompanying code:
Results:
undefinedDaffy DuckBugs Bunny
undefined
Code:
// Function to call when selected items are ready
function callAccepted() {
getSelected().done(function(varObjects) {
for (var k in varObjects) {
console.log(varObjects[k]);
}
});
}
// Function to get selected items from list view
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 = [];
var arrItems = [];
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(){
var itemLength = 0;
var itemObjects = [];
for (var j = 0; j < items.length; j++) {
itemObjects = items[j].get_item("Title");
itemLength += itemObjects;
arrItems.push(itemObjects);
}
dfd.resolve(arrItems, itemLength);
},
function(){
dfd.reject(args.get_message());
}
);
});
return dfd.promise();
}