0

i want to concatenation string having some calculated values in between, so i tried the code below:

var html = '<tr>' + 
          function () {
              var tds = '' 
              array.each(function (i, ele) { tds += '<td>' + i + '</td>' });
              return tds;
          } +
          '</tr>';

But its not working. The function i used in concatenation as treating like a string itself !. Is there any way to do that ? Thank you

1
  • You are defining function but not calling it. Commented Jun 5, 2014 at 6:49

1 Answer 1

5

You need to use an immediately-executing function:

var html = '<tr>' + 
      (function () {
          var tds = '' 
          array.each(function (i, ele) { tds += '<td>' + i + '</td>' });
          return tds;
      })() +
      '</tr>';

But you don't need this function, you can use existing functions to do what you want:

var html = '<tr>' + array.map(function(i, ele) {
    return '<td>' + i + '</td>';
}).join() +
    '</tr>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.