Filter one array with another

痞子三分冷 提交于 2019-12-11 14:59:09

问题


I know this been answered multiple times but I can't seem to find an answer based on my situation. I have two arrays:

words ['word1', 'word2', 'word3']
texts [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]

I am trying to filter the two arrays and return true if there is a match

I have seen several examples of simple arrays of numbers but that does not apply here.


回答1:


You can iterate texts using Array.prototype.some() and check for matches in words using Array.prototype.includes(). This is O(nm) time complexity if the length of words and texts are n and m respectively.

const words = ['word1', 'word2', 'word3']
const texts = [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]

console.log(
  texts.some(
    ({ description }) => words.includes(description)
  )
)

Another solution that's O(n + m) time complexity uses Set.prototype.has() instead, but this approach will likely be negligibly faster or even a little slower if words is a small array, so only use this if words is extremely large.

const words = new Set(['word1', 'word2', 'word3'])
const texts = [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]

console.log(
  texts.some(
    ({ description }) => words.has(description)
  )
)

Update

To address your issue regarding case-sensitivity, I recommend a somewhat different approach. Since both arrays will contain words with mixed casing, convert one of the arrays to regular expressions and test each regular expression against all the words in the other array with the case-insensitive flag.

const words = ['word1', 'WORD2', 'Word3']
const texts = [
    {name: 'blah', description: 'Word4'},
    {name: 'blah2', description: 'Word1'},
    {name: 'blah3', description: 'Word5'}
]

console.log(
  texts.some(({ description }) => {
    const regexp = new RegExp(description, 'i')      
    return words.some(word => regexp.test(word))
  })
)

If your words contain non-alphanumeric characters as well, I highly suggest you borrow this function to escape your regular expressions properly. If you don't escape them, you could potentially end up with cases like below causing your code to throw errors or provide incorrect results.

function escapeRegExp(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

const words = ['?WORD1', 'word2', 'Word3']
const texts = [
    {name: 'blah', description: 'Word4'},
    {name: 'blah2', description: '?Word1'},
    {name: 'blah3', description: 'Word5'}
]

console.log(
  texts.some(({ description }) => {
    const regexp = new RegExp(escapeRegExp(description), 'i')      
    return words.some(word => regexp.test(word))
  })
)



回答2:


You can use filter and includes together to know which word is present in the object

var words= ['word1', 'word2', 'word3']
var texts=[
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]
console.log(texts.filter((x)=>words.includes(x.description)).length>0?true:false)



回答3:


I think this will solve your issue:

const words = ['word1', 'word2', 'word3']
const texts = [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]

function check(arra, arrb) {
return !!texts.find(_text => words.includes(_text.description))
}

console.log(check(words, texts))


来源:https://stackoverflow.com/questions/54950633/filter-one-array-with-another

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