请确保只添加一次内容,可以通过检查数组中是否已经存在内容来实现。例如:
const myArray = [1, 2, 3]; const newValue = 4; if (!myArray.includes(newValue)) { myArray.push(newValue); } console.log(myArray); // 输出 [1, 2, 3, 4]
上一篇:Array.Push不在数组末尾添加
下一篇:array.push没有按顺序推送对象,而一些对象使用await执行操作。