从数组中删除空字符串

use*_*419 2 javascript arrays node.js

我知道还有其他页面,但我想让自己的工作,我不知道为什么它不起作用.我是node.js的新手.

for (var index in output)
{
    if (opt.options.showEmpty != true)
    {
        var check = arrayIsEmpty(output[index]);

        if ( check == true )
        {
            continue;
        }
        else
        {
            var array = removingEmptyString(output[index]);

            console.log(index + "\t" + array);
            //console.log(index+ "\t" + output[index]);
        }
    }
}

function removingEmptyString(array)
{
    var newArray;

    for( var i = 0; i < array.length; i++)
    {
        if(array[i] != "" || array[i] != null)
        {
            newArray[i] = array[i];
        }
    }

    return newArray;
}
Run Code Online (Sandbox Code Playgroud)

我的结果是树,,,我以前在我写的代码之前得到了.现在我得到一个错误

    newArray[i] = array[i];
                                ^
TypeError: Cannot set property '0' of undefined
    at removingEmptyString (librarySeeker.js:130:18)
    at result (librarySeeker.js:76:19)
    at /async/lib/async.js:226:13
    at async/lib/async.js:113:25
    at async/lib/async.js:24:16
    at async/lib/async.js:223:17
    at /async/lib/async.js:510:34
    at IncomingMessage.<anonymous> (pull.js:295:10)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
Run Code Online (Sandbox Code Playgroud)

bev*_*qua 6

你可以.filter在Array的原型中使用该方法.

var pirate = ['a','1','',0];

function arr (value) {
  return value.filter(function (item) {
    return item !== '';
  });
}

arr(pirate);
// <- ['a','1',0]
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您可能需要考虑将回调命名为 .filter

var pirate = ['a','1','',0];

function worthy (value) {
  return value !== '';
}

pirate.filter(worthy);
// <- ['a','1',0]
Run Code Online (Sandbox Code Playgroud)