I’m trying to load text of all divs that have a particular class into an array, but this
var temp = $('.theClass').text();
temp = temp.toArray();
console.log(temp);
keeps giving me the error
Uncaught TypeError: Object has no method 'toArray'
And
var tempArr = [];
var temp = $('.theClass').text();
for (var t in temp){
tempArr.push(t);
}
console.log(tempArr);
results in an array filled with many, many objects within objects just filled with integers.

An explanation of how to do this properly can be found here, but I wonder if someone could provide me with an explanation for why I get these errors. Thanks!
You can use
mapto iterate over each element of the matched set and return some data (in this case, the text). You can then usegetto convert the resulting jQuery object into an actual array:Your first attempt fails because the
textmethod returns a string, and strings don’t have atoArraymethod (hence your “Object has no method” error).Your second attempt fails because you’re iterating over the string with the
for...inloop. This loop iterates over the characters of the string. Each iterationtis assigned the index of the character, so you end up with an array, with one element for each character in the string. You should never really be using afor...inloop for iterating over anything other than object properties, and even then, you should always include ahasOwnPropertycheck.