使用pop()删除数组报错,data.pop is not a function
const dateRemind = _.get(chartData,'reply.result.dateRemind','');
let notimeOut = dateRemind;
notimeOut.pop();
console.log('notimeOut',notimeOut);
报错:Uncaught TypeError : notimeOut.pop is not a function
分析报错原因:pop只能删除数组,notimeOut是否是数组呢,notimeOut由dateRemind复制,dateRemind的_.get的最后一个是默认值,在上述代码里是'',是一个字符串,当数据没请求到时,dateRemind(notiemOut)是一个'',不是一个数组,所以.pop()报错,应该将默认值改为[].
const dateRemind = _.get(chartData,'reply.result.dateRemind',[]);
let notimeOut = dateRemind;
notimeOut.pop();
console.log('notimeOut',notimeOut);
注意:在报错上方打印。
const dateRemind = _.get(chartData,'reply.result.dateRemind','');
let notimeOut = dateRemind;
console.log('notimeOut',notimeOut); // 在这里打印将会看到notimeOut不是一个数组,而是一个空字符串
notimeOut.pop();
//获取数组的最后一个数据
const timeout = dateRemind.slice(-1);
slice截取数据为什么没报错呢,因为slice可以截取数组,也可以截取字符串。
在上述中,let notimeOut = dateRemind;是为了不改变dateRemind的值,删除数组,原数组dateRemind也改变了,这里出现了深浅拷贝的问题。