• 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:

  • Multiple Choice Quiz
  • Check if the user color scheme preference is dark
  • Reverse the order of items in an array
  • Perform a shallow merge of two or more objects

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers