在Vue3中,你可以使用Array.prototype.includes()方法来查找数组中是否包含指定的值。下面是一个示例:

const array = [1, 2, 3, 4, 5];
const targetValue = 3;

console.log(array.includes(targetValue)); // 输出:true

如果你需要获取指定值在数组中的索引位置,可以使用Array.prototype.indexOf()方法。下面是一个示例:

const array = [1, 2, 3, 4, 5];
const targetValue = 3;

console.log(array.indexOf(targetValue)); // 输出:2(索引从0开始)

如果你想要获取指定值在数组中的所有索引位置,可以使用Array.prototype.reduce()方法。下面是一个示例:

const array = [1, 2, 3, 4, 3, 5];
const targetValue = 3;

const indexes = array.reduce((acc, value, index) => {
  if (value === targetValue) {
    acc.push(index);
  }
  return acc;
}, []);

console.log(indexes); // 输出:[2, 4](索引从0开始)

希望对你有所帮助

vue3如何在数组中查找指定值

原文地址: https://www.cveoy.top/t/topic/hUur 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录