gpt4 book ai didi

javascript - 检查数组是否包含给定元素

转载 作者:行者123 更新时间:2023-11-30 06:59:49 25 4
gpt4 key购买 nike

我有一个数字数组,并在 for 循环中向该数组动态添加新数字。但是我需要避免添加数组中已经存在的值。是否有一种 JS native 方法可以在不执行第二个嵌套循环的情况下检查数组中是否存在某些值。我不想使用嵌套循环,因为数组的大小可能会变化到 10000

最佳答案

只需使用includes

var array1 = [1, 2, 3];
console.log(array1.includes(2)); // true

关于javascript - 检查数组是否包含给定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8240371/

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