gpt4 book ai didi

Javascript 未检测到数组中的数字 79 或 16

转载 作者:行者123 更新时间:2023-11-30 14:45:22 26 4
gpt4 key购买 nike

我正在使用 if(!(results[count].m._id in usedIdArray)) 来确定 ID 值是否已存在于数组中。此 if 语句位于循环遍历 results 中的 21 个节点的 for 循环中。 usedIdArray 只是一个整数数组,results[count].m._id 是一个数字。 results[count].m 一般是这样的:

Node {
_id: 79,
labels: [ 'Block' ],
properties:
{ blockID: '674511',
upvotes: '4',
x: '771.2391282095244',
y: '224.80278118474385',
type: 'link',
url: 'https://stackoverflow.com' } }

在 usedIdArray 中还有其他重复的数字,if(!(results[count].m._id in usedIdArray)) 似乎可以很好地检测到这些数字。出于某种原因,只有那些 _id 是 79 和 16 的节点才会导致问题。我知道有重复的 3、1 和其他几个数字。当我运行时:

console.log(results[count].m._id);
console.log(usedIdArray);

我得到:

3
[ 1 ]
79
[ 1, 3, 2, 4, 13, 14, 16 ]
16
[ 1, 3, 2, 4, 13, 14, 16, 79, 17 ]
79
[ 1, 3, 2, 4, 13, 14, 16, 79, 17, 16, 18 ]
79
[ 1, 3, 2, 4, 13, 14, 16, 79, 17, 16, 18, 79, 19 ]
79
[ 1, 3, 2, 4, 13, 14, 16, 79, 17, 16, 18, 79, 19, 79, 20 ]
79
[ 1, 3, 2, 4, 13, 14, 16, 79, 17, 16, 18, 79, 19, 79, 20, 79, 21 ]
[ 1, 3, 2, 4, 13, 14, 16, 79, 17, 16, 18, 79, 19, 79, 20, 79, 21, 79, 22, 23, 24 ]

...最后一行是完整的usedIdArray。我尝试将 id 值解析为整数和字符串,但无济于事。

最佳答案

问题在于 in 运算符,如果指定的属性在指定的对象或其原​​型链中,则该运算符返回 true。在 docs 中阅读更多内容

您可以使用 array.includes 函数来测试元素是否包含在列表中。


var list = ['one', 'two', 'three', 'fourth'];

console.log('one' in list); // -> false
console.log(list.includes('one')); // -> true

关于Javascript 未检测到数组中的数字 79 或 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49014744/

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