javascript - 如何遍历这种 JavaScript 数组?
问题描述
我正在将数据返回到我的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;
}
}
解决方案
您实际上可以通过以下方式.map(...)
在数组上使用 a (假设):row.categories_names
render: function(data, type, row, meta) {
return row.categories_names.map(function(item) {
return '<ul><li>'+item+'</li></ul>';
});
}
它可能更简单。让我知道它是否有效。
推荐阅读
- sql - 查询最近日期
- r - 如何应用机器学习技术/如何使用模型输出
- python - Python 从一个运行多个脚本
- sql - SQL - 具有多个日期的条件窗口
- ios - 将 Gif 从 URL 加载到 CollectionViewCell
- python - 如何从 seaborn 的 KDE distplot 对象中提取 bin?
- sql - .Select with EF Core 在视图中返回 ModelError
- node.js - NodeJs:Sequelize(版本 - 5+)N:M 关联不起作用
- php - 如何为 arangodb-php 中的流式事务增加“maxTransactionSize”
- python - Python:在对数刻度的两条曲线之间拟合圆,以连接圆的原点