学堂 学堂 学堂公众号手机端

在pel数组中查找特定元素的位置或索引的方法是什么

lewis 3年前 (2022-08-30) 阅读数 6 #技术

您可以使用indexOf()方法来查找特定元素在pel数组中的位置或索引。

例如,如果您想要查找元素"apple"在pel数组中的位置,可以使用以下代码:

var pel = ["banana", "apple", "orange", "grape"];
var index = pel.indexOf("apple");

console.log(index); // 输出结果为 1,表示"apple"在数组中的位置为1

如果pel数组中不存在要查找的元素,indexOf()方法将返回-1。


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门