I have a JSON and I want to get data from JSON using conditions in javascript.
When loading the page, the Product HTML table is loading. In that table tbody, there is a td which has an id.
Here I want to do if JSON product_id value equals to that tds id value print currency_symbol to that td. I have mentioned my tried code below.
Products HTML table:
<table id="table" class="table table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>Currencies</th>
</tr>
</thead>
<tbody>
{{#each products}}
<script>
$(document).ready(function(){
insert_items_onload({{id}});
});
</script>
<tr>
<td>{{this.id}}</td>
<td>
<div>
<table class="cur_symbol">
<tbody>
</tbody>
</table>
</div>
</td>
</tr>
{{/each}}
</tbody>
</table>
Final output should be:
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Currencies</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td id='1'>
<div>
<table class="cur_symbol">
<tbody>
<tr><td>€</td></tr>
<tr><td>£</td></tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td>2</td>
<td id='2'>
<div>
<table class="cur_symbol">
<tbody>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td>3</td>
<td id='3'>
<div>
<table class="cur_symbol">
<tbody>
<tr><td>$</td></tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td>4</td>
<td id='4'>
<div>
<table class="cur_symbol">
<tbody>
<tr><td>€</td></tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
Please help me to solve this problem. Thank you.
scripttag that is not properly closed, variablescurrency_nameandratethat are not defined,... You probably want to pass the wholeitemobject toinsert_items_symbol, and take those properties from there. These seem quite basic errors, compared to the task you are trying to achieve. Did you debug at all??jsontag (and word) in your question. There is no JSON explicitly mentioned, only a JavaScript Object literal. JSON should be reserved for the data exchange format, not for literals in JavaScript code. Read thejsontag usage description, especially the part in capitals.IfJSONproduct_idequals to thetdid. Thattdshould displaycurrency_symbolin a table. That was the thisng I tried there.