0

My web app is taking in arbitrarily large 2D arrays that sometimes look something like this:

var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];

I want to write a function to take out every array inside of multiArray that is comprised entirely of quotes. In other words, any array that looks like this:

["","","",""]

should be deleted from multiArray.

I tried writing the following function, but the problem with using splice in a for loop is that splicing will change the length of the array, and I end up trying to access undefined elements. Please help!

Thanks!

Here's the incorrect function I wrote:

function cleanWhitespace(arrayOfArrays) {
    var i;
    var arrayOfArraysLength = arrayOfArrays.length;
    for (i = 0; i < arrayOfArraysLength; i++) {
        var cleanedArray = $.grep(arrayOfArrays[i], function(element) {
                                return element != ""
                            });
        if (cleanedArray.length == 0)  {
            arrayOfArrays.splice(i, 1);
        }
    }
    return arrayOfArrays;
};
1
  • iterate in reverse, or do i-- every time you do a .splice(). This is a very common issue. Commented Jul 15, 2014 at 17:22

5 Answers 5

4

You can use $.grep :

multiArray = $.grep(multiArray, function(v){
    return v.join('');
});

Fiddle : http://jsfiddle.net/scZcB/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Very elegant solution.
1

on the fly:

var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];
var outputArr = removeQuoteArrays(multiArray);
console.log(outputArr);

function removeQuoteArrays(arr) {
    var outputArr = [];

    for (var i = 0; i < arr.length; i++) {
        var currArr = arr[i];
        var isAllQuotes = true;
        for (var j = 0; j < currArr.length; j++) {
            if (currArr[j] != "") {
                isAllQuotes = false;
                break;
            }
        }

        if (!isAllQuotes) {
            outputArr.push(currArr);
        }
    }
    return outputArr;
}

Here's a JSFiddle.

Comments

1

Create a new array instead.

// Only add if...
cleanedArray = multiArray.filter(function(arr){
    // Some elements are not blank
    return arr.some(function(e){ return e !== "" }) 
})

2 Comments

A bit more succinctly: var cleanedArray = multiArray.filter(function(arr){return arr.some(String)});
That's not always possible. Sometimes there are multiple references to the original Array, which means they won't see the update.
0

I added a length check to your function to break out of the loop if the index reaches the array length:

if (i >= arrayOfArrays.length)
      break;

Which makes:

function cleanWhitespace(arrayOfArrays) {
    var i;
    var arrayOfArraysLength = arrayOfArrays.length;
    for (i = 0; i < arrayOfArraysLength; i++) {
        var cleanedArray = $.grep(arrayOfArrays[i], function(element) {
                                return element != ""
                            });
        if (cleanedArray.length == 0)  {
            arrayOfArrays.splice(i, 1);
            if (i >= arrayOfArrays.length)
                break;
        }
    }
    return arrayOfArrays;
};

Comments

0
var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];

function cleanWhitespace(arrayOfArrays) {
    for (var i = 0; i < arrayOfArrays.length; i++) {
        var emptyElements = 0;

        for (var j = 0; j < arrayOfArrays[i].length; j++ ) {
            if (arrayOfArrays[i][j] === "") {
                emptyElements++;
            }
        }

        if (emptyElements === arrayOfArrays[i].length) {
            arrayOfArrays.splice(i, 1);
        }
    }

    return arrayOfArrays;
}

console.log(cleanWhitespace(multiArray));

http://jsfiddle.net/4Jfr9/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.