Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:02:44+00:00 2026-05-27T22:02:44+00:00

I have this code, that takes a string and splits it into an array:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:02:45+00:00Added an answer on May 27, 2026 at 10:02 pm

    When there is only one element in the string (no commas), the variable “nodes” does not become an array, but a regular variable.

    A) Variables referring to arrays are regular variables. B) That’s not how split works. split always returns an array. If the delimiter isn’t present in the string, the resulting array is one element in length. (Live proof) So as long as data returns a string, nodes will be set to an array. Note, though, that data does not always return a string, and so data("nodes").split(",") may fail with the error that split is not a function, because data can return null or an object as well as string. If you know it will always be a string because of your application logic, that’s fine, but if so nodes will always be an array, which is why I mention it.

    Re your comment below: Iteration works just fine on single-element arrays: http://jsbin.com/ehucop/2

    I suspect you need to look at the JavaScript console in your browser, I bet you’ll find an error occurring which is preventing your iteration code from running at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code that takes care of command acknowledgment from remote computers,
I have this code that performs an ajax call and loads the results into
So, I have this code that grabs a bunch of data from the database,
I have this function that takes some user-submitted HTML code from the database: function
I have the following code, which splits up a Vector into a string vector
I have this part of code that takes a file and puts it in
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.