0
<script >
    document.write("<table border=2 width=50%"); 
    for (var i = 0; i < ${SALE_DATA.SALE_ORDER_ITEMS_LIST.length}; i++) {  
      document.write("<tr>");
      document.write("<td>" + ${SALE_DATA.SALE_ORDER_ITEMS_LIST[i].WEIGHT} + "</td>");
      document.write("<td>" + i + "</td>");
       document.write("</tr>");
    } 
    document.write("</table>"); 
</script>

I am trying to pass the i in ${SALE_DATA.SALE_ORDER_ITEMS_LIST[i].WEIGHT} here SALE_DATA is the constant array in angular 5. but it is giving the error like below

ERROR ReferenceError: i is not defined. suggest me the way to pass the i value in loop

if i keep the ${SALE_DATA.SALE_ORDER_ITEMS_LIST[0].WEIGHT} the output is like below

thanks in advance!! out put

2
  • Maybe this can help you: stackoverflow.com/questions/17326796/… Commented Mar 22, 2018 at 9:55
  • What kind of syntax is ${} outside of a template string? You should get Uncaught SyntaxError: Unexpected token { as an error Commented Mar 22, 2018 at 10:13

2 Answers 2

1

you can you use template literal (``) to create a string with dynamic variables in it.

for (var i = 0; i < `${SALE_DATA.SALE_ORDER_ITEMS_LIST.length}`; i++) {
  console.log(i)
}

Above code will work fine but it's not recommended to use template literals in this manner.

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

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

1 Comment

Yeah for (var i = 0; i < ${SALE_DATA.SALE_ORDER_ITEMS_LIST.length}; i++) { console.log(i) } is working fine, but when we try to access i in ${SALE_DATA.SALE_ORDER_ITEMS_LIST[i].WEIGHT it producing the above error...... Thanks for the reply @ Nikx
-2

You may use an intenal iterator , like using forEach loop. This will allow you not taking care of how the loop works.

Something like this can do the job.

 ${SALE_DATA.SALE_ORDER_ITEMS_LIST}.forEach(function(curVal,i){
      document.write("<tr>");
  document.write("<td>" + curVal.WEIGHT + "</td>");
  document.write("<td>" + i + "</td>");
   document.write("</tr>");
 })

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.