这是我的代码,This isCurrent?InvoiceIdStored 总是返回 false,无论我为 id 设置什么值
const invoiceIds = [4,2];
const isCurrentInvoiceIdStored = invoiceIds.includes(id => id === 4);
我想要的是检查给定的数字是否在这个数组中?
答案 0 :(得分:0)
您必须使用 invoiceIds.includes(4)
而不是 invoiceIds.includes(id => id === 4)
。
答案 1 :(得分:0)
Array.prototype.includes()
要搜索的值作为参数,但您将回调函数作为参数传递:
const invoiceIds = [4,2];
const isCurrentInvoiceIdStored = invoiceIds.includes(4);
console.log(isCurrentInvoiceIdStored);
OR:您可能想使用 Array.prototype.some()
,它接受一个函数来测试每个元素
const invoiceIds = [4,2];
const isCurrentInvoiceIdStored = invoiceIds.some(id => id == 4);
console.log(isCurrentInvoiceIdStored);