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?
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);
}
}
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(',');
}
}
undefined I suppose!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.
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.
nullvalues?obj[i].toString().split(",")should fix this.