10

I have ten arrays with empty value

onTable[0 to 10];

look

["Jean5", "Jean3", "Paul2", "Jean6", "", "Paul4", "Jean", "peirre4", ""]
["Paul5", "peirre6", "peirre3", "", "Jean4", "Paul", "peirre5", "Jean2", ""]
...

I want get length of each array without empty value and without create ten variables for check that.

I have test this solution count empty values in array but i dont want make ten variable. ie: count1, count2,....

I check too compare two arrays based on length: skip empty values but is not what i want.

If possible, I want this look like

onTable[0].length(exclude(""))

What is the good way for make that?

3
  • Use filter() and remove empty value then get its length Commented Nov 19, 2015 at 11:40
  • i dont want remove value from array Commented Nov 19, 2015 at 11:41
  • filter will not update the existing array.... it's just return the new array Commented Nov 19, 2015 at 11:42

4 Answers 4

25

Use filter with Boolean to filter non-empty elements from sub-array and use length on it.

onTable[0].filter(Boolean).length

As empty string is falsy in JavaScript, it'll be removed from filtered array.

Demo:

var arr = [
  ["Jean5", "Jean3", "Paul2", "Jean6", "", "Paul4", "Jean", "peirre4", ""],
  ["Paul5", "peirre6", "peirre3", "", "Jean4", "Paul", "peirre5", "Jean2", ""]
];

var len = arr[1].filter(Boolean).length;
document.write(len);

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

Comments

1

You can use filter function for your need: check value to undefined or null etc.

var arr = [
  ["Jean5", "Jean3", "Paul2", "Jean6", "", "Paul4", "Jean", "peirre4", ""],
  ["Paul5", "peirre6", "peirre3", "", "Jean4", "Paul", "peirre5", "Jean2", ""]
];

var len = arr[1].filter(function(x){ return x != ""}).length;
document.write(len);

Comments

1

With a prototype:

Array.prototype.lengthWihtoutEmptyValues = function () {
    var initialLength = this.length;
    var finalLength = initialLength;

    for (var i = 0; i < initialLength; i++) {
        if (this[i] == "") {
            finalLength--;
        }
    }

    return finalLength;
}

var arrays = [
  ["Jean5", "Jean3", "Paul2", "Jean6", "", "Paul4", "Jean", "peirre4", ""],
  ["Paul5", "peirre6", "peirre3", "", "Jean4", "Paul", "peirre5", "Jean2", ""]
];

var arrayLength = arrays[0].lengthWihtoutEmptyValues();

$("#arrayLength").html(arrayLength);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="arrayLength"></div>

Comments

0

You should avoid "wasting" memory and inducing excessive GC. You can reduce() each sub array to a count of it's non-empty values:

sub.reduce(function(prev, cur) {
  return prev + (!!cur);
}, 0);

In order to process the entire master array, you can map() it to lengths:

var arr = [
  ["Jean5", "Jean3", "Paul2", "Jean6", "", "Paul4", "Jean", "peirre4", ""],
  ["Paul5", "peirre6", "peirre3", "", "Jean4", "Paul", "peirre5", "Jean2", ""],
  ["Just1", "", ""]
];

var lengths = arr.map(function(sub) {
  return sub.reduce(function(prev, cur) {
    return prev + (!!cur);
  }, 0);
});
document.write('[' + lengths.join('], [') + ']');

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.