react 选中数组中的几条数据遍历拿到数据中的某一个属性然后判断当符合条件则返回true
可以使用Array.prototype.some()方法来实现这个需求。Array.prototype.some()方法会遍历数组中的每个元素,当某个元素满足指定条件时,返回true,否则返回false。
假设要遍历的数组是data,要判断的属性是property,判断条件是condition,可以这样写代码:
const isPropertySatisfied = data.some(item => item.property === condition);
如果isPropertySatisfied为true,则表示数组中至少有一条数据的属性满足条件;如果isPropertySatisfied为false,则表示数组中没有任何数据的属性满足条件。
以下是一个完整的示例:
const data = [
{ id: 1, name: 'John', age: 20 },
{ id: 2, name: 'Jane', age: 25 },
{ id: 3, name: 'Bob', age: 30 }
];
const condition = 25;
const isAgeSatisfied = data.some(item => item.age === condition);
console.log(isAgeSatisfied); // 输出 true,因为数组中存在年龄为 25 的数据
在这个示例中,我们遍历了data数组中的每个元素,并判断其age属性是否等于condition。由于数组中存在age为25的数据,所以最后输出true
原文地址: https://www.cveoy.top/t/topic/ioNd 著作权归作者所有。请勿转载和采集!