迭代DataTables Json Data时的数组索引

时间:2014-05-03 07:34:23

标签: javascript jquery arrays json datatables

我的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": []}}
]

我正在使用dataTables,如下面的代码

$("#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是“带地图(对象)的数组”, 我想像json的数组索引一样

sitepoint为0, Flippa是1, 谷歌是2

{
    "mData": "url",
    "mRender": function ( url, type, full )  {
    return  '<a href="'+url-ArrayIndex+'?ArrayIndex">' + url + '</a>';
}

1 个答案:

答案 0 :(得分:0)

如果您不关心IE8,那么您可以使用Array.map(您可以使用polyfill for IE8,请参阅链接):

indexpos = myArray.map(function(e){return e.name;})。indexOf('SitePoint');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map