-2

How to remove null values from a array. For example

var data = [
    ["George L. Bunting", null, null, null],
    ["Marc G. Bunting", null, null, null],
    ["Suzanne F. Cohen", null, null, null],
    ["Rosalee  Davison", null, null, null],
    ["Richard  Davison", null, null, null],
    ["Marilynn K. Duker", null, null, null],
    ["Dale  McArdle", null, null, null],
    [null, null, null, null],
    [null, null, null, null],
    [null, null, null, null]
]

I required to get like this

data = [
    ["George L. Bunting", null, null, null],
    ["Marc G. Bunting", null, null, null],
    ["Suzanne F. Cohen", null, null, null],
    ["Rosalee  Davison", null, null, null],
    ["Richard  Davison", null, null, null],
    ["Marilynn K. Duker", null, null, null],
    ["Dale  McArdle", null, null, null]
]

and remove space between words 'Dale McArdle'-> 'Dale McArdle'?

$("input[id*=btnListGeneratorwithDetails]").click(function() {
    $("[id*=hdnfldSpreadSheetData]").val(JSON.stringify(handsontable.getData()));

    strEntityList = JSON.stringify(handsontable.getData());
    //alert(strEntityList);
});
Tekercs
  • 25
  • 1
  • 7
0143.NetUser
  • 29
  • 1
  • 13
  • 1
    You can find another duplicate here: http://stackoverflow.com/questions/12745800/how-to-remove-null-values-from-an-array-using-jquery – LaVomit Feb 10 '16 at 08:13
  • That is a problem. A big one. Try and [ask again](http://stackoverflow.com/help/how-to-ask) when you run into trouble. – dasdingonesin Feb 10 '16 at 08:14
  • Why jQuery? If it's jQuery, why did you not tag it as such? Also, please ask one question at a time. Anyway, where you stuck on this? Do you not know how to loop over the array? Do you not know how to access each element, and check if it's all nulls? Did you not know how to remove an unwanted element? –  Feb 10 '16 at 08:20
  • torazaburo - how to remove unwanted element [null, null, null, null] jquery or javascript – 0143.NetUser Feb 10 '16 at 08:34
  • Possible duplicate of [Remove empty elements from an array in Javascript](http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript) – twernt Feb 10 '16 at 14:48

1 Answers1

1

You can use a combination of .filter and .every:

var data = [
    ["George L. Bunting", null, null, null],
    ["Marc G. Bunting", null, null, null],
    ["Suzanne F. Cohen", null, null, null],
    ["Rosalee  Davison", null, null, null],
    ["Richard  Davison", null, null, null],
    ["Marilynn K. Duker", null, null, null],
    ["Dale  McArdle", null, null, null],
    [null, null, null, null],
    [null, null, null, null],
    [null, null, null, null]
]
data = data.filter(function(entry) {
    return !entry.every(function(value) {
        return value === null;
    });
});
console.log(data);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176