2

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
1
  • 1
    Not sure I get this at all, as there is nothing about how the array looks, what is supposed to be removed, nesting, what all the functions inside the loop does etc. but is seems like just array = array.filter(function() { return this.trim().length; }); should do exactly what you're trying to do ? Commented Nov 29, 2013 at 18:57

3 Answers 3

6

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]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.