gpt4 book ai didi

javascript - reverse() 不是一个函数

转载 作者:行者123 更新时间:2023-11-28 04:11:43 25 4
gpt4 key购买 nike

你好,我在 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/

最佳答案

您需要向 .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]
}

关于javascript - reverse() 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46215260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com