gpt4 book ai didi

javascript - 如何实现 Ruby 的 Array.include?在 JavaScript 中?

转载 作者:数据小太阳 更新时间:2023-10-29 04:11:02 28 4
gpt4 key购买 nike

我有一个数组 tempArray = ["Kathmandu","Pokhara","Dharan"] .为确保“Pokhara”在 tempArry 中,我必须使用循环并检查 tempArray 的每个元素。

有没有办法实现 Ruby 的 Array.include?这样我就不需要使用循环了?

最佳答案

您可以使用 Array.indexOf搜索值:

var includePokhara = ( tempArray.indexOf("Pokhara") >= 0 );

很遗憾,Array.indexOf 未在 Internet Explorer 中实现,但您可以在 StackOverflow 上查看 how to add it back .

关于javascript - 如何实现 Ruby 的 Array.include?在 JavaScript 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203680/

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