我的 json 数据如下所示:
[
{"name": "Sitepoint", "url": "http://sitepoint123.com", "editor": {"name": "John Doe", "phone": ["9191919", "1212121"], "email": []}},
{"name": "Flippa", "url": "http://flippa.com", "editor": {"name": "Adam Smith", "email": ["adam.smith@domain.com"], "phone": []}},
{"name": "Google", "url": "http://google.com", "editor": {"name": "Google Smith", "email": ["Google.smith@domain.com"], "phone": []}}
]
我正在使用像下面的代码这样的数据表
$("#example").dataTable({
"bServerSide": true,
"sAjaxSource": "http://localhost123.com/data_source.json",
"aoColumns": [{
"mData": "name",
"sTitle": "Site name"
}, {
"mData": "url",
"mRender": function(url, type, full) {
return '<a href="' + url + '">' + url + '</a>';
}
}, {
"mData": "editor.name"
}, {
"mData": "editor.phone"
}, {
"mData": "editor",
"mRender": function(data) {
return data.email.join("");
}
}]
});
我想要的只是它正在填充的 Json 的数组索引 上面的json是“Array with Maps(objects)”, 我想要像 json 的数组索引
站点点为0, 弗利帕是 1, 谷歌是 2
{
"mData": "url",
"mRender": function ( url, type, full ) {
return '<a href="'+url-ArrayIndex+'?ArrayIndex">' + url + '</a>';
}
最佳答案
如果你不关心 IE8,那么你可以使用 Array.map (你可以使用 IE8 的 polyfill,请参阅链接):
indexpos = myArray.map(function(e) { return e.name; }).indexOf('SitePoint');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
关于javascript - 迭代 DataTables Json 数据时的数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441578/