-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);
});
5
  • 1
    You can find another duplicate here: stackoverflow.com/questions/12745800/… Commented Feb 10, 2016 at 8:13
  • That is a problem. A big one. Try and ask again when you run into trouble. Commented Feb 10, 2016 at 8: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? Commented Feb 10, 2016 at 8:20
  • torazaburo - how to remove unwanted element [null, null, null, null] jquery or javascript Commented Feb 10, 2016 at 8:34
  • Possible duplicate of Remove empty elements from an array in Javascript Commented Feb 10, 2016 at 14:48

1 Answer 1

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);

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

2 Comments

i'm not getting answer
@0143.NetUser SO doesn't support console.log. Try it your self instead.

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.