Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js indexof

作者:嗜神魔影   发布日期:2026-01-25   浏览:56

// 示例代码:使用 indexOf 方法查找数组或字符串中某个元素的索引位置

// 数组示例
const fruits = ['apple', 'banana', 'orange', 'grape'];
const indexInArray = fruits.indexOf('orange');
console.log(indexInArray); // 输出: 2,表示 'orange' 在数组中的索引是 2

// 字符串示例
const str = "Hello, world!";
const indexInString = str.indexOf('world');
console.log(indexInString); // 输出: 7,表示 'world' 在字符串中的起始索引是 7

// 如果没有找到元素,indexOf 返回 -1
const notFoundIndex = fruits.indexOf('watermelon');
console.log(notFoundIndex); // 输出: -1,表示 'watermelon' 不在数组中

上一篇:js trycatch

下一篇:js self

大家都在看

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 数组对象合并

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站