JavaScript indexOf 方法详解:数组与字符串查找

作者:小括号时间:2026-10-31 04:30:30浏览:629

本文详细介绍JavaScript中indexOf方法的用法,涵盖数组和字符串两种调用场景。通过语法解析与代码示例,说明如何指定起始位置进行检索,并展示在Vue项目中利用indexOf实现标签多选功能的实际应用案例。

一、数组调用

  • indexOf() 方法用于返回数组中指定元素首次出现的索引位置。
  • 该方法会从数组起始位置开始遍历,检查是否存在目标元素。若未指定 start 参数,则从索引 0 开始检索;若指定了 start,则从该位置开始。一旦找到匹配的元素,即返回其第一个出现的位置索引。
  • 若遍历结束后仍未找到指定元素,则返回 -1。
//语法
// array.indexOf(item,start) 
//item 必须 要查找的元素的位置,
//start 非必须可选的整数参数。规定在数组中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
let food= ["番茄", "胡萝卜", "排骨", "苹果"];
let a = food.indexOf("苹果");
console.log(a) // 3
let b= food.indexOf("香蕉");
console.log(b) // -1

二、字符串调用

  • indexOf() 方法用于返回指定子字符串在源字符串中首次出现的位置索引。
  • 注意:该查找过程区分大小写。
  • 若指定的子字符串未在源字符串中出现,则该方法返回 -1。
//语法
//string.indexOf(value,start)
// value  必须 要查找的元素的位置
// start 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 string.length - 1。如省略该参数,则将从字符串的首字符开始检索。
let str="Hello world!";
console.log(str.indexOf("Hello"));//0
console.log(str.indexOf("World") );//-1
console.log(str.indexOf("world"));//6

三、应用例子

此方法可用于实现多项选择功能。

一、数组调用

<template>
    <div class="biaoqian">
                <button v-for="(item,index) in biaoqianList" 
                :key='index' 
                class="btn" 
                type="default" 
                size="mini"
                :class="{'active': isChange.indexOf(index)!=-1}" 
                @click="clickBtn(index)">{{item}}</button>
        </div>
</template>
export default{
        data(){
            return{
                isChange:[],  //多选
                biaoqianList:['早餐','午餐','晚餐','宵夜'],
                foodChose:[]
              }
            },
        methods:{
                clickBtn(index){
                    // 多选
                    if (this.isChange.indexOf(index) == -1) {
                        if(this.isChange.length == 4){
                            uni.showToast({
                                title:'最多选择四项',
                                icon:'none'
                            })
                        }else{
                            this.isChange.push(index);//选中添加到数组里
                        }
                    } else {
                        this.isChange.splice(this.isChange.indexOf(index), 1); //取消选中
                    }
                    console.log(this.isChange)
                    // let biaoqianList =  []
                    // for(let index in this.isChange){ //biaoqianList里面的索引重新加入
                    //     biaoqianList.push(this.biaoqianList[this.isChange[index]])
                    // }
                },
        }
}
<style lang="less">
    .biaoqian{
                display: flex;
                justify-content: start;
                align-items: center;
                .active{
                    background-color: #76d2f4 ;
                    color: white;    
                }
                .btn{
                border:0.01px solid #76d2f4;
                background-color:white ;
                color: #76d2f4;
            }
        }
</style>

到此这篇关于js的indexOf方法使用的文章就介绍到这了,更多相关js indexOf内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

作者最新文章