简体   繁体   中英

Having Trouble Parsing Results of Array

I'm having a bit of trouble trying to parse the results of an array and print to the console. It's a two part problem actually. When I build the array it's adding "undefined" to the results. When I try to loop through the individual strings in the array it isn't parsing, just returning the full array object.

What I'm trying to do is collect all the field values selected from a list view and write them to another child list as separate items. When displaying results in a console it shows as an object array. When I run the typeof method against it I believe it shows as a string.

To reiterate, why am I getting undefined and why is my array not printing to console correctly. Below is an example of what is being returned thus far (when two records are selected) and my code.

Results:

undefinedDaffy DuckBugs Bunny

undefined

Code:

// Grabs selected items from getSelected function and passes parameters to writeSelected function
function callAccepted() {
    getSelected().done(function(varObjects) { 
        for (var k in varObjects) {
            console.log(varObjects[k]);
        }

    }); // End getSelected
} // End callAccepted

// Grabs selected items, accepts input from callAccepted or callRejected functions
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);
        } // End for
        clientContext.executeQueryAsync(
            function(){ // Return to button click 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(){ // Return to button click function
                dfd.reject(args.get_message());
            }
        ); // End ClientContext
    }); // End dfd
  return dfd.promise();
} // End getSelected

Why are you writing "var itemObjects;" in 1 line and add one string "itemObjects += items[j].get_item("Title");" in another? There'll be only 1 string anyway, so when you change those 2 lines into one, "undefined" should disappear:

function callAccepted() {
    getSelected().done(function(varObjects, iLength) { 
    // Stuff
                for (var k = 0; k < iLength; k++) {
                        console.log(varObjects[k]);
                }
    }); // End getSelected
} // 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 = [];
        var arrItems = [];
        for (var i in SelectedItems) {
            var id = SelectedItems[i].id;
            var item = targetList.getItemById(id);
            clientContext.load(item, "Title");
            items.push(item);
        } // End for
        clientContext.executeQueryAsync(
            function(){ // Return to button click function
                for (var j = 0; j < items.length; j++) {
                    var itemObjects = items[j].get_item("Title");
                    var itemLength = items.length;
                    arrItems.push(itemObjects);
                }
                dfd.resolve(arrItems, itemLength);
            },
            function(){ // Return to button click function
                dfd.reject(args.get_message());
            }
        ); // End ClientContext
    }); // End dfd
  return dfd.promise();
} // End getSelected

The reason for this is that after creating the variable without any value, it's undefined, so += 'Unicorn' will give us ugly 'UndefinedUnicorn'. If you wish to make variable for this purpose, write it "var x = ''".

And if - for example - you want to sum length of all "items", then this one function should look like:

        function(){ // Return to button click function
            var itemLength = 0;
            for (var j = 0; j < items.length; j++) {
                var itemObjects = items[j].get_item("Title");
                itemLength += itemObjects;
                arrItems.push(itemObjects);
            }
            dfd.resolve(arrItems, itemLength);
        }

But I'm not exactly sure what are you trying to get here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM