I am trying to convert this array into a string but it gives me the error:
Object [object Array] has no method 'split'
I am converting to a string, so it shouldn't have that problem, I'm not sure why I am getting this error.
My code is:
function preSubmit(){
var optionTexts = [];
$("section").each(function(){
var h2 = $(this).find("h2").text();
optionTexts.push(h2);
$("ol li", this).each(function() { optionTexts.push($(this).text()); });
});
var optionTextString = optionTexts.toString();
var splitText = optionTextString.split(",");
console.log(splitText);
return splitText;
}
The returned value of typeof splitText gives me [object Array], but I expect string.
Reuben Mallaby
5,7574 gold badges51 silver badges47 bronze badges
1 Answer 1
And it's true, array doesn't have any split method. You're messing Join and Split methods, one belong to array, the other to string functions.
What you want is:
var splitText = optionTextString.join(",");
answered Jun 13, 2013 at 13:51
Oscar
14.1k9 gold badges54 silver badges84 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Felix Kling
optionTextString should be optionText.lang-js
splitdoes.optionTexts.join('\n')oroptionTexts.join('<br>').