Array.prototype.indexOf()
应用数据库索引搜索要删掉的二维数组原素的数据库索引,随后应用拼凑删除索引。
const array = [2, 5, 9];console.log(array);const index = array.indexOf(5);if (index > -1) { array.splice(index, 1);}// array = [2, 9]console.log(array);
拼凑的第二个主要参数是要清除的要素总数。一定要注意,拼凑会改动当政二维数组,并回到包括已清除原素的新二维数组。
为了更好地详细考虑,这里有一些涵数。第一个涵数只删掉一个配对项(即从[2,5,9,1,5,8,5]中删掉第一个配对项5),而第二个涵数删掉全部配对项。
function removeItemOnce(arr, value) { var index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); } return arr;}function removeItemAll(arr, value) { var i = 0; while (i < arr.length) { if (arr[i] === value) { arr.splice(i, 1); } else { i; } } return arr;}// Usageconsole.log(removeItemOnce([2,5,9,1,5,8,5], 5))console.log(removeItemAll([2,5,9,1,5,8,5], 5))
在TypeScript中,这种涵数能够加上种类主要参数。
function removeItem(arr: Array, value: T): Array { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); } return arr;}
Array.prototype.filter()
var value = 3var arr = [1, 2, 3, 4, 5, 3]arr = arr.filter(function(item) { return item !== value})console.log(arr)// [ 1, 2, 4, 5 ]
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明。