2
for(var i=0;i < obj.length; i++){
obj[i].split(",");
}

above code gave me split of undefined, it's because my last item of obj is an array that look like this

[""]

How to solve this problem?

5
  • What else does the array contain? Numbers, or only strings/undefined? Can it contain null values? Commented Dec 7, 2015 at 4:30
  • 1
    Can you give us a sample array of obj? As you said, the last item was an array. But in that case it should return .split is not a function not split of undefined Commented Dec 7, 2015 at 4:33
  • You don't have to iterate over the entire length...Also your loop body won't mutate the strings within your object. Commented Dec 7, 2015 at 4:34
  • Make everything to be String, obj[i].toString().split(",") should fix this. Commented Dec 7, 2015 at 4:39
  • Good Question! Though when I tried same over fiddle, it was returning empty string to me rather than split on undefined. This could be a version issue though. See my answer below... Commented Dec 7, 2015 at 7:42

5 Answers 5

1

Iterate through items if and only it is of type string and obj[i] is true value

var obj = ['', null, 100, undefined, 'abc,test'];
for (var i = 0; i < obj.length; i++) {
  if (typeof obj[i] === 'string' && obj[i]) {
    var test = obj[i].split(",");
    console.log(test);
  }
}

Fiddle here

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

Comments

0

If your array doesn't contain numbers (like zero) or boolean values (like true), then you can do this:

for( var i = 0; i < obj.length; i++ ) {
  ( obj[i] || "" ).split(",");
}

Comments

0

It seems to me you are referring to an empty string rather than undefined, please correct me if I'm wrong on that. If it is the "undefined" value which is different than an empty string, providing what your list is would be helpful. If it's really an empty string, just use an if condition to skip it.

for (i = 0; i < obj.length; i++) {
    if (obj[i] != '') { 
        obj[i].split(','); 
    } 
}

2 Comments

Empty string is not undefined I suppose!
Hmm... I'm actually assuming she means an empty string given her output, when I do a quick test, I get the same output, but I could be wrong. I'll write that in my answer.
0

First off all , what you are doing has no purpose with your code.

Namely ;

var obj = { a : "there,are,commas", b : "no commas" } ; 
// next line of code doesnt do anything, 
//just returns undefined on console.
obj["a"].split(",");

Bunch of undefined s are because of this fact. On console, of course.

// if you want to use it, you might have assigned it to a variable
var someVariable = obj["a"].split(",");

So your problem might be something different.

Comments

0

You should be calling toString on your array or more prominently make everything string by using toString method on obj[i].

var obj = ["23",[""]]
for(var i=0;i < obj.length; i++){
  if(!obj[i].toString()==""){
   console.info(obj[i].split(","));
  }
}

Fiddle below for practical.

https://jsfiddle.net/stm0qemn/1/

Comments

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.