简体   繁体   中英

Array not being passed into function

I am trying to iterate through an array, it check if each element is empty. If ALL the elements in the array are empty along with output[library] is empty then I do not want to print out anything. If the array contains a single element in it I want to print that element out. I have tried this so far but the program has a bug of it is not passing in the parameter output to arrayIsEmpty which is causing an error, I also do not know how to iterate through each element, for example first run could have an item in the array, I want to print it, then i want it to continue from the last position, so check if position two is empty etc. (I hope that made sense what I am trying to get through).

    for (var library in output)
            {
                if (opt.options.showEmpty != true)
                {
                    console.log("It has made it to here 1 ");
                    var check = arrayIsEmpty(output);
                    console.log("It has made it to here 2 ");
                    if ( check == false && output[library] == "" )
                        {
                        console.log("It has made it to here 3 ");
                            continue;
                        }
                    else
                        {
                        console.log("It has made it to here 4 ");
                            console.log(library+ ",[" + output[library]+"]");
                        }
                }
                //console.log(library+ ",[" + output[library]+"]");
            }
        }
    });
}

function arrayIsEmpty(attributes)
{
    console.log("IT HAS MADE IT INTO ARRAYISEMPTY");
    for(var i = 0; i < attributes.length; i++)
    {
        console.log("IT IS NOW IN THE FOR LOOP");
        if (attributes[i] == "")
            {
                console.log("IT SHOULD BE FALSE HERE");
                return false;
            }
        else
            {
            console.log("IT SHOULD BE TRUE HERE");
                return true;
            }`enter code here`
    }

I put in console.log statements, it is reading "It has made it to here 1" then It has made it into arrayISEmpty" then "It has made it to here 2" then "It has made it to here 4" so it is not returning either true or false

You need to move the arrayIsEmpty outside the for loop.

console.log("It has made it to here 1 ");
var check = arrayIsEmpty(output);

for(var library in output)
{
    //...
}

Otherwise it will not be executed when output.length == 0 .

You arrayIsEmpty function won't do what you want it to do either.

function arrayIsEmpty(obj) {

    // Check if array has any items
    var isEmpty = obj.length == 0;

    if(isEmpty) {
        return true;
    }

    isEmpty = true;

    // Make sure each item is not empty
    for(var element in obj) {
        var item = obj[element];
        isEmpty = isEmpty && !!item;
    }

    return isEmpty;
}

Not sure which type output really is - it is used like an object ( for var library in output ) as well as an array ( for var i... attributes[i] ).

Assuming it's an array, you can iterate over all elements like so (this should be equivalent to the stuff in your arrayIsEmpty , I hope, except for the log s):

attributes.forEach (function (attr) {
    return (attr != "");
});

But I am not sure what you are trying to do - true does not mean that the array is really empty. It'll rather give you the information whether the first item in the array is something different than an empty string.

But, as the other people have already pointed out, it seems that output is in fact empty, ie, contains no elements. You can check that easily by

if (output.length === 0) {...

(Yes, that's three =s. For the reason, look at Javascript type coercion.)

All the best, nobi

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