0

I am trying to populate the following string if I have n data:

group.items[0].fname || group.items[1].fname ||..........|| group.items[n].fname

I have implemented following, I would like to know is there a better way to do that?

nam="";
for (i = 0; i < data.length; i++) { 
  if(i==0)
    nam="groups.items["+i+"]"+".fname";
  else
    nam=nam+"||"+"group.items["+i+"]"+".fname";
 }
4
  • 1
    Why do you need such string? We can't help you without knowing what exactly you are trying to achieve. If you are evaling that string you are doing something terribly wrong. Commented Jun 2, 2015 at 20:43
  • jsfiddle.net/opL8dq26 look at series.name format, is it clear? Commented Jun 2, 2015 at 21:00
  • 1
    So you are using kendo templates? Why not jsfiddle.net/6qnd510o? Commented Jun 2, 2015 at 21:22
  • It is pretty good approach, however, if you take a look at legend, there are some extra commas coming to the picture. I need to investigate. The reason that I asked under javascript tag, because I have observed not many from kendo developers in SOF. Commented Jun 2, 2015 at 22:26

1 Answer 1

1

Since data is an array you can simply use Array.prototype.map to build array of substrings and then join them:

var data = [1,2,3];

var nam = data.map(function(el, i) {
    return 'group.items[' + i + '].fname';
}).join('||');

alert(nam);

Sign up to request clarification or add additional context in comments.

3 Comments

@mystackoverflow Just curious, now that you have the string, what do you plan to do with it? Print it out and put it up on the wall?
@torazaburo, i need it for the following jsfiddle.net/opL8dq26 , look at series.name , is it clear enough?
@mystackoverflow That would have a great thing to put in your question.

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.