[英]How to loop through this kinda javascript array?
我將數據返回到我的DataTables
表,但我無法遍歷以下數組:
["BREAKFAST (Vegetables)", "LUNCH-DINNER (Vegetables)"]
我嘗試了下面的代碼,但它只是打印第一項
{data: null,
render: function(data, type, row, meta) {
var categoriesNamesList = '';
//loop through all the row details to build output string
for (var item in row.categories_names) {
var r = row.categories_names[item];
//Check if r is NULL or Empty then skip
if(r){
categoriesNamesList = '<ul><li>'+ r + '</li></ul>';
}
}
return categoriesNamesList;
}
}
在循環內移動返回的位置。 根據您的代碼,我幾乎可以打賭您只獲得最后一項。
render: function(data, type, row, meta) {
var categoriesNamesList = '';
//loop through all the row details to build output string
for (var item in row.categories_names) {
var r = row.categories_names[item];
//Check if r is NULL or Empty then skip
if(r){
categoriesNamesList = '<ul><li>'+ r + '</li></ul>';
return categoriesNamesList;
}
}
}
您可以通過以下方式在數組上使用.map(...)
(假設為row.categories_names
):
render: function(data, type, row, meta) {
return row.categories_names.map(function(item) {
return '<ul><li>'+item+'</li></ul>';
});
}
它可能更簡單。 如果有效,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.