简体   繁体   中英

How to iterate data in table from multi dimensional array

I currently having trouble in iteration of data to table from multidimension array.

Here is my string.

str = 'stack%splitoverflow,php%splitcodeigniter'

My logic here is first, im gonna split the string from , delimiter.

Here is the result.

result_array = ['stack%splitoverflow', 'php%splitcodeigniter']

Next, I'll gonna split each array value from %split delimiter

Now, My question is how im gonna put that value in table like this:

| col1             | col2        |
------------------------------
| stack            | overflow    |
| php              | codeigniter |
---------------------------------

If you are asking how to log this as a table, you can use console.table() :

 let str = 'stack%splitoverflow,php%splitcodeigniter'; let separated = str.split(',').map(s=>s.split('%')); console.table(separated);

I created an example for your question .I splitted your object values and made another object and finally bind obj value to table. Hope this helps.

 $(document).ready(function(){ var result_array = ['stack%splitoverflow', 'php%splitcodeigniter']; $.makeArray(result_array); $.each( result_array,function(index,obj){ var data=obj.replace("%split",","); var newobj=data.split(","); $(".table").append("<tr><td>"+newobj[0]+"</td><td>"+newobj[1]+"</td></tr>") }); });
 <!DOCTYPE html> <html> <head> <link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div id="tblContainer"> <table class='table table-bordered'> </table> </div> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM