Search through a nested array to find a matching element in a separate array

Looking to find a match between values in two arrays consisting of arrays.

let arr1 = [[1,3,5],[2,4,7],[1,5,9]] // [false, false, true]
let arr2 = [1,2,4,5,9] // Should return true as arr2 includes all values in arr1[2].

Need the function to return true if all values in an arr1[i] are present in arr2.

for (let i = 0; i < arr1.length; i++) {
  if (arr2.every(arr1[i])) {
    return true
  }
}

Answer №1

If you want to compare arrays in JavaScript, consider using the .map() method along with .every().

let arr1 = [[1,3,5],[2,4,7],[1,5,9]];
let arr2 = [1,2,4,5,9];
let result = arr1.map(x => x.every(y => arr2.includes(y)));
console.log(result);

Alternatively, you can utilize the .filter() method for getting only matching results:

let arr1 = [[1,3,5],[2,4,7],[1,5,9]];
let arr2 = [1,2,4,5,9];
let result = arr1.filter(x => x.every(y => arr2.includes(y)));
console.log(result);

Answer №2

To achieve a single boolean value, one method is to utilize Array#some in tandem with Array#every for each nested array and verify the elements of array2 using Array#includes.

var array1 = [[1, 3, 5], [2, 4, 7], [1, 5, 9]],
    array2 = [1, 2, 4, 5, 9],
    result = array1.some(a => a.every(v => array2.includes(v)));
    
console.log(result);

An alternative approach involves utilizing a Set.

var array1 = [[1, 3, 5], [2, 4, 7], [1, 5, 9]],
    array2 = [1, 2, 4, 5, 9],
    result = array1.some((s => a => a.every(v => s.has(v)))(new Set(array2)));
    
console.log(result);

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

What is the method for altering the background color of an HTML table cell when a particular event occurs?

As I create an html table, I am looking to dynamically change the background color of specific boxes after running a selenium webdriver. For instance, if the webdriver successfully navigates the site, I want the corresponding box in the table to turn gre ...

Efficiently managing modules with requirejs and Backbone.Marionette

After organizing the file structure of my web app, utilizing RequireJs and Backbone.Marionette, it now looks like this: |- main.js |- app.js |- /subapp1 |- subapp1.js |- subapp1.router.js |- /subapp2 |- subapp2.js | ...

Is there a way to decode/compile/interpret the data within nested HTML tags within my AngularJS directives?

How can I process/compile/resolve the contents of enclosed HTML in my directives. The specific directive being discussed is: angular.module('transclude', []) .directive('heading', function(){ return { restrict: 'E&apos ...

What distinguishes calling the toString() method from simply using it?

After running multiple tests, I have not been able to identify any noticeable distinctions. const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString(); Along with const fruits = ["Banana", "Orange" ...

Holding off $ajax requests until specific code finishes executing

I'm facing an issue with incorporating geolocation data into my $ajax call URL. Currently, both console.log(lat/lon) calls return the initial value of 0, indicating that the geolocation call is too late to provide the updated values. This results in t ...

Learn the process of updating an Array with a list of values based on checkbox selection and dynamic filtering using AngularJS

$scope.selectObjectType = function () { $scope.selected = []; // reset previous selection $scope.model.allItemsSelected = true; // all are selected by default unless... // If any object type is not checked, then uncheck the "allItemsSelected" ...

In JavaScript, navigate to a new page only after successfully transmitting data to the server

Creating a redirect page that sends data to the server before transitioning to a new page can be achieved using JavaScript as shown below. <body> <script type="text/javascript"> **** Discussion of cookie-related transactions **** document.c ...

Issues with HTML5 video playback in Apple machines using the Firefox browser

On a website I'm developing, I've included an HTML5 video that works perfectly except for one specific issue - it won't play on Firefox in Apple devices. Surprisingly, it functions well on all other browsers and even on Firefox in Windows an ...

Getting the selected value from a dropdown menu in ReactJS

I am working on creating a form that resembles the following structure: var BasicTaskForm = React.createClass({ getInitialState: function() { return { taskName: '', description: '', emp ...

From HTML to Mat Table: Transforming tables for Angular

I am currently facing a challenge with my HTML table, as it is being populated row by row from local storage using a for loop. I am seeking assistance in converting this into an Angular Material table. Despite trying various suggestions and codes recommend ...

Webpage Text Animation

This is the visual representation of my webpage: https://i.stack.imgur.com/tYq34.png I am seeking to implement a uniform animation effect for the text "Carlos Qiano" and "Lorem Ipsum", similar to the link below: https://i.stack.imgur.com/XVnBS.jpg Note: ...

Issues with the proper functionality of the .ajax() method in Django

I'm currently facing an issue with my Ajax code while trying to interact with the database in order to edit model instances. I noticed that the first alert statement is functioning correctly, however, the subsequent alert statements are not working as ...

Create a connection between a div and a React component, allowing for the seamless transfer of

I'm looking to implement a feature where clicking on a specific div will redirect the user to another page, similar to React Router. However, I currently lack the knowledge to make it happen. Below is the code snippet in question: const Card: React.FC ...

Changing a global variable via an AJAX call

I seem to be facing a common issue that many others have encountered. Despite my understanding that global variables can be modified inside functions in Javascript, I am struggling with this concept in practice. var lastMessage = 0; function loadChat() { ...

ZeroMQ and Electron integration: Unable to find the specified bindings file

Currently, I am running Windows 7 x64 with nodejs 5.1.0 and Electron 0.35. I carefully followed the instructions provided by the Electron Quick Start app and then proceeded to add the line require("zmq") to the main.js file. However, upon executing npm ins ...

What is the best way to deactivate div elements once an overlay has been applied to them?

My goal is to place an overlay on my form to prevent users from accessing the content. Even though I have added an overlay, users can still interact with input fields. How can I prevent that? .overlay { background: rgba(0, 0, 0, .75); text-align: ce ...

Converting JSON data into CSV format and saving it in a variable: A guide

How can I convert JSON data to CSV format using JavaScript and save it in a JavaScript file? The data includes information about Apple iPhone 4S sales being cancelled in Beijing. Here is a sample of the JSON data: { "count": 2, "items": [{ "title" ...

Is it possible to utilize AJAX to fetch code from a separate file, view, or page in order to create an experience akin to a single-page application

Essentially, I'm aiming to create a simplified version of a Single Page Application within the admin panel of my website. The idea is to organize the information by using tabs, making the panel less cluttered. Here's a rough layout of what I have ...

Obtain a matrix data from an Excel spreadsheet using c++ xll, make necessary modifications, and then return the

I have developed an xll function that takes a matrix from Excel, makes modifications, and returns it: __declspec(dllexport) LPXLOPER12 WINAPI ZZZZUpdateArray1(LPXLOPER12 arrayin) { if (arrayin->xltype == xltypeMulti | xlbitDLLFree) { do ...

Can a CSS hover effect transition modify the color scheme?

I have multiple buttons on a single page, and I want the hover effect to be applied to all of them with just one code using jQuery. Please review my code below. $(document).ready(function(){ $('.button').hover(function(){ var bc=$(this ...