'how to loop inside '' in javascript

I want to loop inside single quote I have nested loop in my code errors occurs when I try to loop inside the quote

var jsonObj = @json($data);
var borFld = Object.keys(jsonObj);
var all_fields;
var fields_remove = []
var table_id = @json($table_id);

var rows = [];
var htmls = [];
function getFields(){
    var my_fields = [];

    for(keys in jsonObj){
        var item = jsonObj[keys];
        var fields = Object.keys(item)

        htmls +=
        '<tr>'+
            for(list in fields) {
                var myfields =   fields[list]
                all_fields = item[myfields]

            }
        '</tr>'
    }
   // $('#tb_time_sheet tbody').html(htmls)

}


Solution 1:[1]

I guess you get the data as string of JSON object. You should first parse it so that JS can read it.

const parsedJsonObject = JSON.parse('{"name":"Test"}');

After that you can use it inside for loop. If it is not array of object and key value pair

  1. You can get values by:

    Object.values(parsedJsonObject);

  2. You can get keys by:

    Object.keys(parsedJsonObject);

Then, iterate over keys and get value by:

parsedJsonObject[key]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1