• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Return an array of elements that appear in both arrays

Return an array of elements that appear in both arrays

Return an array of elements that appear in both arrays

Use Array.prototype.includes() to determine values that are not part of values. Use Array.prototype.filter() to remove them.

const similarity = (arr, values) => arr.filter(v => values.includes(v));

// EXAMPLE
similarity([1, 2, 3], [1, 2, 4]); // [1, 2]

Source

https://www.30secondsofcode.org/js/s/similarity

Miscellaneous

Related Snippets:

  • Insert an element at the end of a set of elements inside a shared parent
  • Find a specific object in an array of objects
  • Wrap an element around a given element
  • Check if all the elements in values are included in arr

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us