0

I'm a beginner in jquery and ajax. While i was going through some example online, i came across the following piece of code and wondered what exactly it does.

    lines = newLine.split('#');

 jQuery.each(lines, function(lineNo, line) {                    
                        eval("linedata = " + line);             
                        data.push(linedata);

                    });

I'm not a programmer, but just trying to understand its functionality. Can anyone help me?

2

2 Answers 2

1

The each function iterates over an array which is supplied as the first parameter. During each iteration the index and element are passed into a function that is performed. The function is passed as the second parameter to the each function.

Read more on the jQuery Documentation

In the example you have provided a string newLine is split into an array using # as the delimiter.

The each function then iterates over the newly created array, assigning the value of each element to a variable linedata and pushes linedata onto another array.

This could be more easily achieved with the following, since the call to eval is unnecessary:

 jQuery.each(lines, function(lineNo, line) {                                
     data.push(line);
 });
0

I pretended, for a moment, that I was a new programmer. This is how you should go about looking into things from here on out:

1.) Ok, I don't know what this first line is doing. It's splitting something (based on the split word). Hmmm let's Google for "split javascript". This is the first thing that comes up. From here, you may be wondering what a String is, so you would search for that as well).

2.) Ok so now I know that splitting a String gives me an array (again you probably looked this up by this step) of the newLine substrings that were separated by the # character. Cool. So let's look into what jQuery.each does. I google "jQuery.each" and this is the first thing that comes up.

Awesome! Now you understand what a String is, an Array, the split function from String as well as what jQuery.each is. :D

EDIT: As you move forward, you'll realize that W3C is generally an inferior source of information. I simply linked to it since it was literally the first thing that came up when I Googled "split javascript". Overall it does the job for giving you a good overview of certain things when you're learning them for the first time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.