找到匹配项后删除数组中所有元素的Lodash方法? [英] Lodash Method for Removing All Elements in Array After Match Found?

查看:302
本文介绍了找到匹配项后删除数组中所有元素的Lodash方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了Lodash文档很多次了,似乎找不到我想要的东西.

I have browsed over the Lodash docs quite a bit and can't seem to find what I'm looking for.

我想知道Lodash是否具有一种方法或简单的方法组合,一旦检测到某个匹配项(包括匹配项本身),就可以删除数组中所有剩余的元素.

I want to know if Lodash has a method or simple combo of methods to remove all remaining elements in an array once a certain match has been detected, including the match itself.

例如:

let blogArray = ["check", "out", "my", "blog", "https", "someblog", "com"]
let matchingEl = "https"
_.doAThing(blogArray, matchingEl)
=> ["check", "out", "my", "blog"]

推荐答案

这可以使用Array对象的indexOf和splice函数来实现.以下是同时使用Lodash和javascript函数的实现

This can be achieved using indexOf and splice functions of an Array object. Below are the implementation using both Lodash and javascript functions

blogArray.splice(0,blogArray.indexOf(matchingEl))

_.slice(blogArray, 0, _.findIndex(blogArray, function(k){return k == matchingEl;}) 

这篇关于找到匹配项后删除数组中所有元素的Lodash方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆