JavaScript: Comparing an array of arrays with multiple identical elements to another array and returning a unique array of arrays

const array1 = [[1, 2, 3], [1,3,4], [1,2,5]];
let b = []
let c = [2,3]

  array1.forEach(e => {
    c.some(r => {
     if(e.includes(r))
    b.push(e)
  })
})

console.log(b)

Upon running the code, the output was [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 3, 4 ], [ 1, 3, 5 ] ]

However, the expected result should have been [ [ 1, 2, 3 ]]

Answer №1

Utilize filtering on the original array and leverage Array.every() along with Array.includes() to validate that all elements in c are present in the subArray:

const array1 = [[1,2,3], [1,3,4], [1,3,5], [1,2,5]];
const c = [2,3]

const result = array1.filter(subArray => 
  c.every(n => subArray.includes(n))
)

console.log(result)

You can also employ the latest Set functionalities to verify if c is a subset of subArray:

const array1 = [[1,2,3], [1,3,4], [1,3,5], [1,2,5]];
const c = new Set([2,3])

const result = array1.filter(subArray =>
  c.isSubsetOf(new Set(subArray))
)

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 correct method for saving two numpy arrays into a text file with appropriate formatting?

I'm looking to generate an input file with a specific format (21 rows and 20 columns). 0. 2900. 0. 2900. 0. 2900. 100. 2900. 100. 2900. 100. 2900. 200. 2900. 200. 2900. 200. 2900. 300. 3600. 300. 3600. 300. 3600. Below is the code I've be ...

I added an onClick event to an SVG image, however, I am struggling to get the event to execute a jQuery if/else statement

I've been working on creating an SVG map of the US, and I'm almost finished. The final step involves implementing a simple if-else statement to prompt users for the name of the state when they click on it. My goal is to fill the state green if th ...

Utilize the native HTML attribute to capture the mouse wheel event

I'm interested in utilizing the mousewheel event in my project, but all the information I've found online relies on addEventListener(). I want to detect it using native HTML and CSS. In simpler terms, I'm hoping for something along the lines ...

Generating intricate JSON structures with JavaScript programming instructions

In the world of JSON, I am still a novice. I need to use JavaScript to construct the JSON structure below, but I'm struggling with adding the second element ("12101") and populating the people in the JSON Structure. Below is the code I tried, however, ...

Modifying button text with jQuery is not feasible

I'm facing a challenge with jQuery and I need the help of experienced wizards. I have a Squarespace page here: . However, changing the innerHTML of a button using jQuery seems to be escaping my grasp. My goal is to change the text in the "Add to car ...

Substituting characters, backslashes, and double quotes may not function properly

Trying to replace a single backslash \ with two \\, but encountering issues with double quotes. var json = { "DateToday": "2021-08-11", "MetaData": [ { "id": "222" ...

JavaScript nested function scope loss issue is being faced

Could someone provide an explanation of the scope binding in this code snippet? window.name = "window"; object = { name: "object", method: function() { nestedMethod: function() { console.log(this.name); ...

How can I prevent text highlighting on a website?

Is there a way to lock the copy button on my site without restricting the save as button, which is activated by right click? I want users to be able to save the website as an HTML file, but prevent them from copying text. Can this be achieved using Javas ...

Is it possible to add items to a JS object that isn't an array?

Here is an example of the object I am working with: { "_id": "DEADBEEF", "_rev": "2-FEEDME", "name": "Jimmy Strawson", "link": "placeholder.txt", "entries": { "Foo": 0 } } To access this data in my JavaScript code, I use a $.getJSON call. ...

How can a PHP script send responseText to AJAX from POST request? I am looking to send a message back with Ajax

I am currently working on setting up a system where Ajax calls upload.php and expects a success or failure message in return. I attempted to use echo "success" in the php script, anticipating that xhr.responseText would capture the message. However, when I ...

Getting an error message of 'Unable to locate Firebase Storage Default Bucket on the server?

I'm currently facing an issue with the server not being able to locate the bucket. To troubleshoot, I've stored the token and other crucial details in a separate file as a string. Afterwards, I split it and utilize the relevant text in my Javascr ...

Exploring the different pages in React js

I am currently working on implementing a button in React Js that, when clicked, should navigate to another screen. However, I keep encountering an error: TypeError: Cannot read property 'push' of undefined. I have tried various solutions but noth ...

Is it possible to specify the timing for executing Typescript decorators?

One issue I've encountered is that when I define a parameterized decorator for a method, the decorator runs before the method itself. Ideally, I'd like the decorator to run after the method has been called. function fooDecorator(value: boolean) ...

What steps can I take to improve the functionality of the slide navigation feature

I am currently working on implementing a sliding menu feature. The menu can slide open smoothly, however, I am encountering an issue when trying to close it by clicking on the 'x' button. let openNav = document.querySelector(".slideOpen"); ...

Exploring the capabilities of chromaprint.js within a web browser

I'm currently in the process of developing a music streaming platform and I'm looking to include the chromaprint.js library for deduplication purposes. In my workflow, I utilize browserify and gulp. Despite the fact that the library claims it ca ...

Audio Playlists and Dynamic Loading with WordPress Shortcode

I'm looking to incorporate the wp_audio_shortcode() function using AJAX, and then customize the style of the audio player. However, I'm facing an issue where the HTML code returned by the AJAX request does not allow me to customize the audio play ...

Utilizing an EllipseCurve for Extrusion Paths in Three.js - How is it Done?

The issue: I'm struggling to convert an EllipseCurve into a path that can be extruded along in Three.js. When I try to use the EllipseCurve as the extrude path, nothing shows on the screen even though there are no errors. However, if I switch it to a ...

Sending Rails form data to a JavaScript click event

I'm currently working on a user profile form with a "Submit" button that triggers some client-side validations before proceeding. In my code, I've set up an intercepting click event on the submit button to run the validations and then proceed wi ...

What is the best way to structure a data object in Javascript or VueJs?

As I work on developing a small application using VueJs, the data I receive is structured in a particular format: { "interactions":[ { "id":14, "user_id":1, "schedule":"2017-06-04 05:02:12", "typ ...

Nodemon fails to restart: [nodemon] attempting restart because of modifications

I ran the command: npm run server Despite my attempts to find a solution, I am still puzzled as to why the results are not working. Even after globally installing npm install -g nodemon, the server still does not restart automatically and only displays me ...