Lodash Method for Removing All Elements in Array After Match Found?

岁酱吖の 提交于 2019-12-13 22:27:01

问题


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

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.

For example:

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

回答1:


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;}) 



回答2:


This is endearing to me

_.doAThing(blogArray)

Because it so boldly embodies a technique of programming called wishful thinking – it captures this idea that your program is as elegant as you want it to be; make a magic wand, wave it on some arguments, and your program works! And it does, we just have to write _.doAThing...

We can easily traverse an array using recursion. To design a recursive function, we have a base case, and inductive cases

  • base case – items is empty, return an empty array
  • inductive case 1 – items is not empty, therefore we have at least one element – if the first item matches 'https', we're done and there are no elements to follow; return empty array.
  • inductive case 2 – the first item does not match, concat the first item with the recursive result of the rest of the items.
const doAThing = (items = []) =>
  isEmpty (items)
    ? []
    : first (items) === 'https'
      ? []
      : [ first (items) ] .concat (doAThing (rest (items)))

Ok, so this is exactly how I would like to express doAThing – the idea is I just keep wishing functions along the way. Now I just need to write isEmpty, first, and rest

const isEmpty = (items = []) =>
  items.length === 0

const first = (items = []) =>
  items [0]

const rest = (items = []) =>
  items.slice (1)

And allakhazam! Like magic, your program is done. Note, we can collapse the first two cases using || because each case returns the same []

const doAThing = (items = []) =>
  isEmpty (items) || first (items) === 'https'
    ? []
    : [ first (items) ] .concat (doAThing (rest (items)))

const isEmpty = (items = []) =>
  items.length === 0

const first = (items = []) =>
  items [0]

const rest = (items = []) =>
  items.slice (1)
    
const data =
  ["check", "out", "my", "blog", "https", "someblog", "com"]
  
console.log (doAThing (data))
// [ 'check', 'out', 'my', 'blog' ]

An obvious improvement would be to make the https match a parameter of the function

const doAThing = (match, items = []) =>
  isEmpty (items) || first (items) === match
    ? []
    : [ first (items) ] .concat (doAThing (match, rest (items)))

console.log (doAThing ('https', data))
// [ 'check', 'out', 'my', 'blog' ]


来源:https://stackoverflow.com/questions/48921597/lodash-method-for-removing-all-elements-in-array-after-match-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!