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

  • SEARCH
  • Home
  • 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 5995895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:01:33+00:00 2026-05-23T00:01:33+00:00

I have a string that has data separated by a pipe character ( |

  • 0

I have a string that has data separated by a pipe character (|).

Example

var somestring = "data1|data2|data3";
var separated = somestring.split("|");

I know how to use the split() to separate each data.

However, I don’t know how many pipes there will be in the resulting Array.

In jQuery or JavaScript, how do I loop over the array returned?

  • 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-23T00:01:34+00:00Added an answer on May 23, 2026 at 12:01 am

    In jQuery or JavaScript, how do I loop through each separated variable?

    You basically just need to iterate over the resulting Array.

    jQuery

    $.each loop

    This method is easy to work with, and benefits in the variables used being encapsulated.

    $.each(separated, function(index, chunk) {
        // `chunk` is each member of the array.
    });
    

    jsFiddle.

    Of course, jQuery is JavaScript so any of the below methods will also work.

    JavaScript

    for loop

    This is the recommended way.

    for (var i = 0, length = separated.length; i < length; i++) {
        var chunk = separated[i];
        // `chunk` is each member of the array.
    }
    

    jsFiddle.

    You’ll notice too the length property is cached so it is not looked up on each iteration. Some browsers already optimise for this, however IE appears to still benefit from it cached. It only takes 5 seconds to do, so you may as well keep IE users happy too.

    You may want to define i and chunk outside of the for loop, because JavaScript has no block scope (unless you’re using let), and those variables will exist before (declaration hoisted) and after (no block scope).

    for ( in ) loop

    This loop is generally not recommended, as it should be used for iterating over object properties only, not array like member properties.

    for (var chunk in separated) {
         if ( ! separated.hasOwnProperty(chunk)) {
             continue;
         }
         // `separated[chunk]` is each member of the array.   
    }
    

    jsFiddle.

    This loop will loop over all properties up the prototype chain, so hasOwnProperty() must be used. For this reason it is not recommended for arrays.

    for ( of ) loop

    This loop is standardized in ECMA 6 and is able to loop over NodeLists and iterators.

    for (var chunk of separated) {
         // `chunk` is each member of the array.   
    }
    

    jsFiddle

    forEach() method

    This method is an addition to the ECMA-262 standard. It’s not available in IE8, but it can be shimmed relatively easily.

    separated.forEach(function(chunk, index) {
         // `chunk` is each member of the array.   
    });
    

    jsFiddle.

    Other specialised methods

    If you’re looking to iterate for a specific goal, it may be useful to use a specialised iterator. Keep in mind these also don’t have the best browser support.

    filter method

    Creates a mew array of the elements which the associated callback returned truthy for.

    separated.filter(function(element) {
        return +element > 255;
    });
    

    reduce method

    Creates a new value based on reducing the elements of the array, from left to right.

    separated.reduce(function(accumulator, element) {
        return accumulator.concat(element);
    }, "");
    

    See also the reduceRight method.

    map method

    Creates a new array, replacing each element with the returned value of the associated callback.

    separated.map(function(element) {
        return element.substr(0, 1);
    });
    

    every method

    Returns a boolean value of which is the result of every element in the array passing the test. This method short circuits, i.e. it returns whenever one element’s callback doesn’t return truthy.

    separated.every(function(element) {
        return element.substr(0, 1) == "a";
    });
    

    some method

    Returns a boolean value of which is the result of some element in the array passing the test. This method short circuits, i.e. it returns whenever one element’s callback passes the test.

    separated.some(function(element) {
        return element.substr(0, 1) == "a";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .Net web forms page, that has a data grid on it
I have a social app that has a user_profile table with typical standard fields,
I'm writing an application that will have a SQL Server backend that will store
So I have a database with 2 tables - Workflows and WorkflowSteps I want
Currently I have one monitor application in production. The job of this monitor is
There is a data structure called NameObjectCollectionBase in .NET which I'm trying to understand.
I'm trying to query an xml column using an IN expression. I have not
I'm considering using Core Data for an app I'm writing. However, after reading the
I am developing an app for WP7. I pull some data out of a
I've got a bit of a problem with T-SQL, I'm basically in a position

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.