Determining if there is a common element in two arrays to yield a true outcome

In my Ionic app built with Angular 1.x, I am dealing with two arrays containing numbers:

var arr1 = [1,32,423,43,23,64,232,5,67,54];
var arr2 = [11,32,1423,143,123,64,2232,35,467,594];

The common numbers in these arrays are 32 and 64.

I need a JavaScript solution that efficiently determines if there is at least one common number between the two arrays.

Below is the code snippet I have written:

angular.forEach(arr1 , function (arr1 , count) {
      if ( inArray(arr1 , arr2) )
      {
        return true;
      }
  });

Answer №1

Utilizing ES6 makes this task simple:

var numbers1 = [1, 32, 423, 43, 23, 64, 232, 5, 67, 54];
var numbers2 = [11, 32, 1423, 143, 123, 64, 2232, 35, 467, 594];

console.log(numbers1.some(num => numbers2.includes(num)));

Answer №2

const numbers1 = [10, 24, 312, 45, 56, 67, 78, 89, 90];
const numbers2 = [15, 34, 412, 55, 66, 77, 88, 99, 100];

function findCommonNumbers(arr1, arr2) {
  let commonFound = false;
  for (let j = 0; j < arr1.length; j++) {
    if (arr2.indexOf(arr1[j]) !== -1) {
      commonFound = true;
      break;
    }
  }
  return commonFound;
}

console.log(findCommonNumbers(numbers1, numbers2));

Answer №3

  • MDN some - stops searching when a match is found and returns a boolean
  • MDN includes - returns a boolean indicating whether an item is present in the array

var arr1 = [1,32,423,43,23,64,232,5,67,54];
var arr2 = [11,32,1423,143,123,64,2232,35,467,594];

var hasDupe = arr1.some(function(val){
   return arr2.includes(val);
});

console.log(hasDupe);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Which specific web framework supports the functionalities of Microsoft Dynamics CRM 2011?

Is there an SDK available from Microsoft that can help me create a web product with similar rich ajax features as Microsoft Dynamics CRM 2011? I have considered using Microsoft SharePoint Foundation 2010, but I am concerned that it is designed for small o ...

What could be causing my script files to not load when I use ajax load() to bring external content into a #content div?

I am currently troubleshooting why my JS files, or script files, are not loading when I bring external content into the #content DIV. While the HTML and CSS load correctly, the scripts do not. Upon page load, my index page, complete with head, body, sc ...

Custom directive to change the background image is not functioning as expected

HTML: <div class="has-bg" back-img="{{currentCoverImg}}" style="min-height: 1000px"> ...... </div> I came across a post discussing the correct method of binding data to a directive and using it in my code. The backImg directive is ...

Scrolling through UL elements using Jquery

I am struggling to implement jQuery for scrolling a UL list, with two span elements for moving up and down. It works fine for a single li child, but how can it work for a dynamically filled ul? Any help would be greatly appreciated, as I am completely lo ...

Encountering a Parsing error while utilizing redux: Unexpected token present

Trying to implement Redux for managing the searchField state on the website. After creating action.js, reducer.js, constant.js, and redux.connect() function, An error occurred: Parsing error: Unexpected token 62 | return <div className=&apo ...

Experience the auditory bliss with Javascript/Jquery Sound Play

I am creating an Ajax-driven application specifically for our local intranet network. After each response from my Ajax requests, I need to trigger a sound in the client's browser. I plan to store a sound file (mp3/wav) on our web server (Tomcat) for ...

Error: selenium web driver java cannot locate tinyMCE

driver.switchTo().frame("tinymce_iframe"); String script="var editor=tinyMCE.get('tinymce_textarea');"; JavascriptExecutor js=(JavascriptExecutor) driver; js.executeScript(script); I'm encountering a WebDriverException while try ...

What is causing the infinite loop when connecting to a Node.js server with Socket.IO?

A small team of two developers is currently working on setting up a Simple Chat Server using node js and socket.io on github. After pulling my partner's changes and running npm start, I noticed that my console was repeatedly outputting "a user is conn ...

Executing an Amplifyjs GET request containing a request body

Is it possible to utilize GET requests with a message body using AmplifyJS? Specifically, I am curious about the process of achieving this functionality with AmplifyJS. While synthetic tests function properly (using Fiddler as my test client), I have enc ...

Unleashing the power of TypeScript with Solid JS Abstract Class

Why am I getting an undefined error for my calcUtilisation method when using an Abstract Class as the type in createStore? Is there a way to utilize a type for the data along with a method within the same class for createStore? abstract class Account { ...

Twilio - encountering difficulties processing phone number upon submission

I clicked on this link to follow the tutorial (you can find it via twilio.) and after completing all the necessary steps, I encountered an issue. Upon running localhost and entering a phone number, I did not receive any text message and the verification wi ...

Is there a way to convert c# code into javascript?

HttpCookie newCookie = Request.Cookies["cookie1"]; int count = (newCookie .Values.Keys.Count) / 6; int keyCount = (newCookie .Values.Keys.Count); string val = HttpUtility.UrlDecode(newCookie .Values.GetKey(i).ToString()); string str = newCookie .Value ...

Challenges accessing all columns of a single model in Rails and AngularJS view

While working on my Rails application, I encountered an issue when trying to retrieve details of the logged-in user using an AngularJS service. The JSON data fetched from the server only displays a few columns instead of all the columns present in the mode ...

What is the best way to pinpoint a specific Highcharts path element?

I am currently working on drawing paths over a graph, and I am looking to dynamically change their color upon hovering over another element on the page - specifically, a list of data points displayed in a div located on the right side of my webpage. Below ...

Vue transition isn't functioning correctly without the specified mode parameter of 'out-in'

I'm struggling to comprehend why the transition doesn't smoothly roll from top to bottom without mode="out-in". When using out-in, it rolls as expected (albeit with a delay), but without it, the transition just suddenly appears after rolling dow ...

Activate a button only when a value is inputted into a text box associated with a chosen radio button

I'm facing a challenge with my radio buttons and sub-options. When a user selects an option, the corresponding sub-options should be displayed. Additionally, I want to enable the next button only when text is entered in all sub-option text boxes for t ...

Scrolling with jQuery just got a sleek upgrade with our new

Can anyone suggest a jQuery scrollbar that resembles the clean black bar found on the iPhone? I need a simple design without visible up or down buttons. Many scripts I came across offer more features than necessary for my project. My div element has a fi ...

Error injecting Angular components

Here is the structure of my HTML file: <html> <head> <title>PLD Interaction pattern</title> <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/> </head> <body ng-app="myT ...

Enhance the performance of node.js when processing data from a massive file

My dilemma lies in the challenge of reading and processing a large file with numerous rows. When dealing with small files under 50kb, everything runs smoothly. However, I am faced with a 15MB file for a programming competition, which serves as a hard input ...

The transition of the element transform between the states of 'relative' and 'fixed' lacks smoothness

On my webpage, I have a banner element with the class .banner and a floating element with the class .float. As the user scrolls, I want the floating element to appear centered vertically relative to the banner when it is in view. However, once the user sc ...