js some()方法

语法

arr.some(callback(element[, index[, array]])[, thisArg])

参数

callback
用来测试每个元素的函数,接受三个参数:
element
数组中正在处理的元素。
index  可选
数组中正在处理的元素的索引值。
array 可选
some()被调用的数组。
thisArg 可选
执行  callback 时使用的  this 值。

返回值

如果回调函数返回任何数组元素的truthy值,则返回true;否则为false




// 一、测试数组元素的值 const a = [11, 50, 40, 3, 5, 80, 90, 4]; const arr = []; a.some((item, index, array) => { console.log(item); // test.html:26 11 // test.html:26 50 // test.html:26 40 // test.html:26 3 // test.html:26 5 // test.html:26 80 // test.html:26 90 if (item > 80) { return arr.push(item); } }) console.log(arr); // [90] function isBiggerThan10(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(isBiggerThan10); // false //二、 判断数组元素中是否存在某个值 // 模仿 includes()方法, 若元素在数组中存在, 则回调函数返回值为 true : var fruits = ['apple', 'banana', 'mango', 'guava']; var fruitsNew = []; function checkAvailability1(arr, val) { return arr.some(arrVal => { val == arrVal }) } checkAvailability1(fruits, 'kela'); // false checkAvailability1(fruits, 'banana'); // true // 三、根据条件截取更新数据 function checkAvailability2(val) { fruits.some((item, i) => { if (item == val) { fruits.splice(i, 1) } }) } checkAvailability2('banana'); console.log(fruits); //  ["apple", "mango", "guava"]

  

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/some

转载于:https://www.cnblogs.com/webcode/p/10413546.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JS的some()方法是用于检测数组中的元素是否满足指定条件的函数。该方法会依次执行数组的每个元素,如果有一个元素满足条件,则返回true,否则返回false。 具体语法为:array.some(function(item, index, arr){}, thisValue)。 其中,item代表数组中正在被处理的元素,index代表该元素在数组中的索引,arr代表正在被处理的数组。thisValue是可选参数,用于指定在执行回调函数时的this值。 需要注意的是,some()方法不会对空数组进行检测,也不会改变原始数组。 通过传入一个回调函数来实现对数组元素进行条件判断,如果回调函数对某个元素返回true,则some()方法立即返回true,不再继续检查剩余的元素。只有当所有元素都不满足条件时,some()方法才返回false。因此,可以利用some()方法来检测数组中是否有满足条件的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JavaScript中的some()方法使用详解](https://download.csdn.net/download/weixin_38574410/13030549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [js的array.some()和array.every()](https://blog.csdn.net/weixin_42265852/article/details/89075440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [JavaScript数组some方法](https://blog.csdn.net/Qiemo_/article/details/124446467)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值