0

Problem

I have a Java list (and I am using Java Server Pages) and I want to iterate over each element of the list in JS (so the index is the JS variable).

Obviously I cannot set the index as a variable and do this : var v = '${object.list[' + index + '].value}'

Instead, I have to write a specific EL for each possible index. ${object.list[0].value} ${object.list[1].value} ...

This is very redundant. I only have a list of 4 elements so I can manage, but if it was 100...

Solution ?

So far I can store the EL as a string, like this : var v = "$" + "{object.list[" + index + "].value}"

Now, can you tell me if I have any means to evaluate this string as an EL ? Like using the eval() function for example (which does not work).

Flyout91
  • 782
  • 10
  • 31
  • What is `${…}`? – Bergi Jun 09 '17 at 13:45
  • Have you tried simply `var v = ${object.list[index].value}`? Don't fool around with strings when you don't have to. – Bergi Jun 09 '17 at 13:46
  • 1
    "*I have a Java list*" - sounds like you want to serialise that to a JSON array, and then iterate that in a normal JS loop. – Bergi Jun 09 '17 at 13:48
  • ${…} is the syntax for an EL with Java Server Pages. I tried but it does not work. Indeed I could use a JSON array but I really don't want to add something else. I feel like there must be a way to achieve this simply. Or at least it should. – Flyout91 Jun 09 '17 at 13:51
  • it is also a valid interpolation in JS template strings. You don't state that you are dealing with jsp in your question... – n00dl3 Jun 09 '17 at 13:53
  • I edited my post concerning JSP. Didn't know it was relevant. – Flyout91 Jun 09 '17 at 13:54

0 Answers0