我在谷歌单子里有逗号分隔的列表,比如“苹果,香蕉”。我需要一个函数将元素添加到这个列表中,如so =SETADD("apple,banana","carrot"),它应该返回值"apple、香蕉、胡萝卜“。
function SETADD(list, item) {
var array = list.split(',');
if (!array.includes(item)) {
return list + ',' + item;
}
else {
return list;
}
}这个函数返回一个错误的TypeError: Cannot find function includes in object apple,banana. (line 3).,它看起来像函数的第一个参数不是字符串吗?我做错了什么?
发布于 2019-05-03 09:01:36
不幸的是,在目前阶段,Array.includes()还不能在Google脚本中使用。所以作为一个解决办法,这个修改怎么样?
发自:
if (!array.includes(item)) {至:
if (array.indexOf(item) === -1) {参考文献:
https://stackoverflow.com/questions/55966256
复制相似问题