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 6577165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:36:05+00:00 2026-05-25T15:36:05+00:00

Obligatory jsFiddle example . When I run an array of strings through jQuery’s $.each

  • 0

Obligatory jsFiddle example.

When I run an array of strings through jQuery’s $.each function, I get what I expect.

$.each(["abc", "123", "def", "456"], function (i, val) {
    $("<li></li>").text("source[" + i + "]: " + val).appendTo(".eachResults");
    // run for each string in the array ("abc", "123", ...)
});

When I run the same array of strings through jQuery Template’s {{each}} operator, though, it treats it as a two dimensional array of chars.

<script id="testTemplate" type="text/x-jquery-tmpl"> 
<ul>
    {{each(i, prop) $data}}
    {{if $data.hasOwnProperty(i)}}
    <li>
        source[${i}]: ${$data[i]}
        {{! run for each char of each string in array (0:"a", 1:"b", 2:"c", 0:"1", 1:"2", 3:"3", ...)}}
    </li>
    {{/if}}
    {{/each}}
</ul>
</script>

$("#testTemplate").tmpl(["abc", "123", "def", "456"]).appendTo(".tmplResults");

Since the i in the template always seems to reference correctly into $data, I don’t really have any clue how this indexing works at all. It seems like i would need to be a two-dimensional index to work correctly, but it doesn’t appear to be (typeof (i) === "number").

Follow-up question

@mblase75 certainly explained the issue here. Unfortunately, given this was a subset of my actual code, it turned out to just bring up a different question about recursively calling an {{each}} template when you come across an array of strings.

  • 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-25T15:36:06+00:00Added an answer on May 25, 2026 at 3:36 pm

    Remember that templates are an implicit loop. Your original {{each}} was looping through each character in each string — the template was looping through each string in the array.

    This will give you the desired result (more or less):

    <script id="testTemplate" type="text/x-jquery-tmpl"> 
        <li>
            source[]: ${$data}
        </li>
    </script>
    

    http://jsfiddle.net/wuEyp/10/ uses the above code. The index is gone because the template doesn’t seem to provide for it at the “root” level.

    http://jsfiddle.net/wuEyp/11 will add the indexing back in using a function. For some reason, I can’t do it properly with a closure.

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

Sidebar

Related Questions

Xcode:Run>Show>Breakpoints I've added the obligatory [NSExceptionRaise] and objc_exception_throw yet when I close the Breakpoints
I'm currently learning OpenGL ES programming on Android (2.1). I started with the obligatory
As a new android programmer I have my obligatory todo app running. It is
First, my obligatory I'm new to rails statement: I'm new to rails. Sorry for
Obligatory - I'm a newbie. Have a job that involves programming and I'm teaching
I recently created a new template page, I put the obligatory <?php /* Template
Given a ResultMap for an iBatis select query, it seems obligatory that all columns
I have been trying to get middleman, compass and formalize to work together, but
I'm building a small Java library which has to match units in strings. For
Is it possible to call typeof(x) within a data rendering template tag? ${typeof(x)} Obligatory

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.