简体   繁体   中英

Removing empty strings from array

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to 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;
}

My result is tree,,, that i was previously getting before the code i wrote. now i get an error of

    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

You could just use the .filter method in Array's prototype .

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

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

arr(pirate);
// <- ['a','1',0]

As an alternative, you might want to consider naming the callback to .filter

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

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

pirate.filter(worthy);
// <- ['a','1',0]

In the spirit of learning, here is a working version of your solution:

function removingEmptyString(array) {
    'use strict';
    var newArray = [];  // don't forget to initialize it
    for( var i = 0, len = array.length; i < len; i += 1) {
        if(typeof array[i] === 'string' && array[i].length > 0) {
            // add the string to the end of the new array
            newArray.push(array[i]);
        }
    }
    return newArray;
}

The error is saying that newArray has not been initialised, so it cannot assign the 0 property to an undefined object.

You can improve your function to make it work:

function removingEmptyString(array){
  var newArray = [];

  for( var i = 0; i < array.length; i++){
    // empty string and null are falsy values by default is js
    if(array[i])
    {
    // use this if you want to keep "undefined" values in the newArray in place
    // of the null ones in the original array
        newArray[i] = array[i];

    // otherwise just push the values in the new array
    // newArray.push(array[i]);
    }
  }

  return newArray;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM