Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active April 30, 2019 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/c139d6ccf93313f06f391b026ef91ac7 to your computer and use it in GitHub Desktop.
Save hygull/c139d6ccf93313f06f391b026ef91ac7 to your computer and use it in GitHub Desktop.
Array.some(func) in JavaScript - StackOverflow solution for https://stackoverflow.com/questions/55905395/search-with-tags-using-includes

array.some(func)

Problem link: https://stackoverflow.com/questions/55905395/search-with-tags-using-includes

Use array.filter(item => item.tag.some(checkTag)).map(item => item.name)

	> let array = [
	...   {
	...     "name": "CoffeShop",
	...     "tag":[
	...       "Coffee",
	...       "Cookies",
	...     ],
	...   },
	...    {
	...     "name": "TeaShop",
	...     "tag":[
	...       "Tea",
	...     ],
	...   },
	...    {
	...     "name": "IceCreamShop",
	...     "tag": [
	...       "Ice Cream",
	...     ]
	...   },
	... ]
	undefined
	> 
	> let tags = ['Coffee', 'Cookies'] 
	undefined
	> 
	> 
	> function checkTag(tag) {
	...     return tags.indexOf(tag) > -1
	... }
	undefined
	> 
	> array.filter(item => item.tag.some(checkTag))
	[ { name: 'CoffeShop', tag: [ 'Coffee', 'Cookies' ] } ]
	> 
	> array.filter(item => item.tag.some(checkTag)).map(item => item.name)
	[ 'CoffeShop' ]
	> 
	> 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment