reverse()不是函数

时间:2017-09-14 09:23:06

标签: javascript arrays sorting

你好我在javascript中的一个函数来计算我们的票务系统的库存。作为输入,我得到以下输入:

[{key:"10-09-2017", value:{countCreatedTickets: 98, countOpenTickets: 13, countSolvedTickets: 61}},
 {key:"12-09-2017", value:{countCreatedTickets:51,  countOpenTickets: 14, countSolvedTickets: 33}}
]

我使用的功能是:

function funnelCalulation(ds){
    var stock = 0, stockCalulations = []
    console.info('-------  funnelCalulation -------')
    console.info(ds)
    ds = ds.reverse()

    for(var i = 0; i < ds.length; i++ ){
        if (i == 0){
            stock = ( ds[i].value.countCreatedTickets + ds[i].value.countOpenTickets ) - ds[i].value.countSolvedTickets
            stockCalulations.push({datum: ds[i].key, createdTickets: ds[i].value.countCreatedTickets, opentTickets: ds[i].value.countOpenTickets, ticketStock: stock })

        }
        if (i > 0){
            stock = ( ds[i].value.countCreatedTickets + ds[i].value.countOpenTickets + stock ) - ds[i].value.countSolvedTickets
            stockCalulations.push({datum:ds[i].key, createdTickets: ds[i].value.countCreatedTickets, opentTickets: ds[i].value.countOpenTickets, ticketStock: stock })
        }
    }

    return stockCalulations[ds.length-1]
}

我按照以下方式调用了该函数:

var stockValues = funnelCalulation(values)
            console.info('  ---- > stockValues  <--------')
            console.info(stockValues)

console.info(ds)返回以下输出: enter image description here

如果我输入typeof ds,则返回该对象。结果是函数工作正常,但返回值时会返回以下错误:

ticketsGraphs.js:49 Uncaught TypeError: ds.reverse is not a function
    at funnelCalulation (ticketsGraphs.js:49)
    at Object.success (createWidgets.js:121)
    at i (jquery-3.2.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
    at A (jquery-3.2.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)

我把完整的代码放在下面的jsfilde中:

https://jsfiddle.net/Erik_Hoeven/Lahuc4oe/

1 个答案:

答案 0 :(得分:0)

您需要传递.reverse()数组。

 function funnelCalulation(ds){
        var stock = 0, stockCalulations = []
        console.info('-------  funnelCalulation -------')
        console.info(ds)
        ds = ds.toString().split('').reverse().join('')

        for(var i = 0; i < ds.length; i++ ){
            if (i == 0){
                stock = ( ds[i].value.countCreatedTickets + ds[i].value.countOpenTickets ) - ds[i].value.countSolvedTickets
                stockCalulations.push({datum: ds[i].key, createdTickets: ds[i].value.countCreatedTickets, opentTickets: ds[i].value.countOpenTickets, ticketStock: stock })

            }
            if (i > 0){
                stock = ( ds[i].value.countCreatedTickets + ds[i].value.countOpenTickets + stock ) - ds[i].value.countSolvedTickets
                stockCalulations.push({datum:ds[i].key, createdTickets: ds[i].value.countCreatedTickets, opentTickets: ds[i].value.countOpenTickets, ticketStock: stock })
            }
        }

        return stockCalulations[ds.length-1]
    }