数组常用方法总结 (5) :find / findIndex / filter

文章详细介绍了JavaScript中的数组方法find、some和findIndex,它们用于检测数组元素是否满足特定条件。find返回符合条件的第一个元素,some返回布尔值表示是否存在符合条件的元素,findIndex返回符合条件的元素索引。在处理对象数组时,这些方法可能会影响原数据。文章通过Vue.js的示例代码展示了这些方法的用法和区别。
摘要由CSDN通过智能技术生成

find

  • 与前边讲过的 some 类似,用于检测数组的每一项是否符合限定条件。
  • 只要遇到一个符合条件的,就会停止循环。
  • 在循环中,如果是简单数组,数据不会被改变,如果是对象数组,数据会改变。
  • 如果停止了循环,后续对数据的改变也会停止。
  • 与 some 不同的是,find 如果遇到了符合条件的元素,返回值为当前元素,而 some 返回的是布尔值 true。如果遍历完数组都没有检测到符合条件的元素,那么 find 返回的是 undefined,some 返回的是 false。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">原始数组</div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="num" label="序号"> </el-table-column>
                <el-table-column prop="value" label=""> </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate"> find </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [
                { num: "0", value: "12" },
                { num: "1", value: "18" },
                { num: "2", value: "53" },
                { num: "3", value: "100" },
            ],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {},
    methods: {
        operate() {
            this.newObject = this.tableData.find((item, index, arr) => {
                item.value = Number(item.value) + 1;
                console.log("index:" + index + ",item:" + item.value);
                return item.value < 50;
            });
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后
打印内容

findIndex

  • 与 find 类似,用于检测数组的每一项是否符合限定条件。
  • 只要遇到一个符合条件的,就会停止循环。
  • 在循环中,如果是简单数组,数据不会被改变,如果是对象数组,数据会改变。
  • 如果停止了循环,后续对数据的改变也会停止。
  • 与 find 不同的是,findIndex 返回的内容为符合条件的元素的索引值。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">原始数组</div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="num" label="序号"> </el-table-column>
                <el-table-column prop="value" label=""> </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate"> findIndex </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [
                { num: "0", value: "12" },
                { num: "1", value: "18" },
                { num: "2", value: "53" },
                { num: "3", value: "100" },
            ],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {},
    methods: {
        operate() {
            this.newObject = this.tableData.findIndex((item, index, arr) => {
                item.value = Number(item.value) + 1;
                console.log("index:" + index + ",item:" + item.value);
                return item.value < 50;
            });
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后
打印内容

filter

  • 使用限定条件,对数组进行过滤操作。
  • 返回值为符合条件的元素的集合,循环不会停止(不管是遇到符合条件的还是不符合条件的)。
  • 在循环中,如果是简单数组,数据不会被改变,如果是对象数组,数据会改变。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">原始数组</div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="num" label="序号"> </el-table-column>
                <el-table-column prop="value" label=""> </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate"> filter </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [
                { num: "0", value: "12" },
                { num: "1", value: "18" },
                { num: "2", value: "53" },
                { num: "3", value: "100" },
            ],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {},
    methods: {
        operate() {
            this.newObject = this.tableData.filter((item, index, arr) => {
                item.value = Number(item.value) + 1;
                console.log("index:" + index + ",item:" + item.value);
                return item.value < 50;
            });
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后
打印内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. findIndex() - 返回数组中满足条件的第一个元素的索引,如果没有符合条件的元素,则返回 -1。 语法:array.findIndex(callback(element[, index[, array]])[, thisArg]) 参数: callback:用来测试每个元素的函数,返回 true 表示当前元素符合条件,false 则不符合。 element:当前遍历到的元素。 index:当前遍历到的元素的索引。 array:数组本身。 thisArg:可选,执行 callback 函数时使用的 this 值。 示例: const arr = [1, 2, 3, 4, 5]; const index = arr.findIndex((element) => element > 3); console.log(index); // 3 2. filter() - 返回一个新数组,其中包含原数组中所有符合条件的元素。 语法:array.filter(callback(element[, index[, array]])[, thisArg]) 参数: callback:用来测试每个元素的函数,返回 true 表示当前元素符合条件,false 则不符合。 element:当前遍历到的元素。 index:当前遍历到的元素的索引。 array:数组本身。 thisArg:可选,执行 callback 函数时使用的 this 值。 示例: const arr = [1, 2, 3, 4, 5]; const newArr = arr.filter((element) => element > 3); console.log(newArr); // [4, 5] 3. reduce() - 通过对数组中的每个元素进行归纳操作,将数组归纳为单个值。 语法:array.reduce(callback(previousValue, currentValue[, index[, array]])[, initialValue]) 参数: callback:归纳操作的函数,包含两个参数: previousValue:先前归纳操作的结果,或者在第一次调用时是 initialValue。 currentValue:当前遍历到的元素。 index:当前遍历到的元素的索引。 array:数组本身。 initialValue:可选,作为归纳操作的初始值。 示例: const arr = [1, 2, 3, 4, 5]; const sum = arr.reduce((previousValue, currentValue) => previousValue + currentValue, 0); console.log(sum); // 15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值