I have this code, that takes a string and splits it into an array:
nodes = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")
When there is only one element in the string (no commas), the variable "nodes" does not become an array, but a regular variable. So when I try to iterate over each element in "nodes", nothing happens if the original string only contains one element. If it has several elements, everything is OK.
$.each(nodes, function (id, node_id) {
if ($("#" + model_id + "-" + node_class + "-" + node_id + "-" + "chkbx").is(":checked")) {
counter ++
}
})
I have tried to declare "nodes" as an array, but when I assign the splitted string, it's all the same. Since I use a "split" to assign values I don't think I can use "push" to append values to the array.
I have tried to put square brackets everywhere, I think, i.e. like this:
[nodes] = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")
... but that didn't help.
Is there any solutions to this, except from checking if "nodes" is an array or not, and then write different code to handle both options?