What is the process for obtaining intersection set data from an array?

I'm trying to find the intersection set within an array only containing type 1 and 2. Can you help me with that?

var arr = [
    {
        id: 1, auths: [
            { authId: 1, type: 1, value: 'Test1' },
            { authId: 2, type: 1, value: 'Test2' },
            { authId: 3, type: 2, value: 'Test3' },
            { authId: 4, type: 2, value: 'Test4' },
            { authId: 99, type: 3, value: 'Test' }
        ]
    },
    {
        id: 2, auths: [
            { authId: 1, type: 1, value: 'Test1' },
            { authId: 2, type: 1, value: 'Test2' },
            { authId: 4, type: 2, value: 'Test4' },
            { authId: 99, type: 3, value: 'Test' }
        ]
    },
    {
        id: 3, auths: [
            { authId: 1, type: 1, value: 'Test1' },
            { authId: 4, type: 2, value: 'Test4' },
            { authId: 5, type: 1, value: 'Test5' },
            { authId: 99, type: 3, value: 'Test' }
        ]
    },
    {
        id: 4, auths: [
            { authId: 1, type: 1, value: 'Test1' },
            { authId: 3, type: 2, value: 'Test3' },
            { authId: 4, type: 2, value: 'Test4' },
            { authId: 99, type: 3, value: 'Test' }
        ]
    }
]

This is the desired output:

var outArr = [
    { authId: 1, type: 1, value: 'Test1' },
    { authId: 4, type: 2, value: 'Test4' }
]

I have attempted the following;

arr.map(p=>p.auths).filter(x=> arr.map(p=>p.auths).includes(x))

and also this;

var _map=arr.map(p=>p.auths);
_map.filter(({authId:id1})=> _map.some(({authId:id2})=> id2!==id1))

I tried a few more things but none of them worked. Is there a way to achieve this without using a loop?

Answer №1

You may implement a solution like this:

var arr = [ { id: 1, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, value: 'Test2' }, { authId: 3, type: 2, value: 'Test3' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 2, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, value: 'Test2' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 3, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 5, type: 1, value: 'Test5' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 4, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 3, type: 2, value: 'Test3' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] } ];

let intersectedArray = arr.map(x => x.auths)[0];
arr.map(x => x.auths).map(arr => {
   intersectedArray = intersectedArray.filter(n => {
      return arr.map(el => {return n.authId === el.authId && n.type === el.type && n.value === el.value;}).includes(true)
   });
})
console.log(intersectedArray)

map each of the auths arrays, then apply another map by filtering with an inner map to compare elements.

If you wish to only include elements of type 1 and 2, you can add a condition in the inner map:

var arr = [ { id: 1, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, value: 'Test2' }, { authId: 3, type: 2, value: 'Test3' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 2, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, value: 'Test2' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 3, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 5, type: 1, value: 'Test5' }, { authId: 99, type: 3, value: 'Test' } ] }, { id: 4, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 3, type: 2, value: 'Test3' }, { authId: 4, type: 2, value: 'Test4' }, { authId: 99, type: 3, value: 'Test' } ] } ];

let intersectedArray = arr.map(x => x.auths)[0];
arr.map(x => x.auths).map(arr => {
   intersectedArray = intersectedArray.filter(n => {
      return arr.map(el => {return n.authId === el.authId && n.type === el.type && n.value === el.value && el.type !== 3;}).includes(true)
   });
})
console.log(intersectedArray)

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

The error message "TypeError: addNewUser is not a function in React.js onSubmit

What could be causing the error message "TypeError: addNewUser is not a function"? The issue arises when I complete the form and click save, displaying the error that addNewUser is not defined as a function. The problem occurs within the following code ...

Ionic app encounters issue fetching local JSON data

I have been working on my Ionic application and I encountered an issue when trying to load local JSON data into it. I set up a http.get in my javascript to retrieve the data, but for some reason it's not showing up in the application. It seems like th ...

What is the best way to send a populated custom class from JavaScript to a .NET MVC app using AJAX?

I am working with a .NET class in C# called BusinessModel: public class BusinessModel { public string BlobName { get; set; } public string NewName { get; set; } } In my MVC Ajax Controller, I have an action called DoBusiness: [HttpPost] public A ...

What is the best way to use a Handlebars file on multiple routes?

I have been working on extracting articles from a news website by scraping them successfully. The data is being displayed properly on the front-end console log, but I am facing an issue with rendering it onto the page using a button - it only appears when ...

Setting the default time zone to UTC for Material UI date and time picker

Looking to implement a dialog in React with Material UI for users to input start and end date/time. The goal is to have the default start set to the beginning of the current day and the default end set to the end of the current day (UTC). However, I'm ...

The Date Filter is causing a glitch in formatting the date value

I have a variable called dateSubmitted with the value of "dateSubmitted": "07-09-20:11:03:30" Currently, I am utilizing Angular Version 7 Within my HTML code, I am using the date filter to format the date like so: <td> {{element.dateSubmi ...

Is extracting the title of an image from Flickr?

I have a JavaScript code that randomly pulls single images from Flickr every time the page is refreshed. Now, I want to add a feature where the title of the image is displayed when it's hovered over. However, I've been searching for a solution fo ...

Enable automatic playback of HTML5 video with the sound on

I want to add an autoplay video with sound on my website, but I'm running into issues with newer browsers like Chrome, Mozilla, and Safari blocking autoplay if the video doesn't have a 'muted' attribute. Is there a clever HTML or Javas ...

Show dynamic HTML Dropdowns with nested JSON data

I've been racking my brains trying to implement this specific functionality in the UI using a combination of HTML5, Bootstrap, CSS, and JavaScript. My goal is to create dropdown menus in the UI by parsing JSON input data. Please Note: The keys withi ...

Javascript detecting key press event only firing once when a key is continuously held down

I have implemented the code below to redirect to a specific page when the w, s, a, d keys are pressed. <script> document.addEventListener('keydown', function(e){ e = e || window.event; key = e.keyCode || e.charCode; var keys = { 87: &ap ...

Inform the user that an error has occurred when attempting to perform an invalid

While using redux promise middleware for my front end, I am wondering about the correct status code to throw from my backend in case of an error. I know that I can use res.status(500).json(something), but is 500 the appropriate code for all types of erro ...

Using Angular JS for Traditional Multi-page Websites

Lately, I've been diving into Angular 2 and I have to admit, it's an impressive framework for building single-page applications. But here's the thing - how would one go about integrating Angular with a traditional website (maybe using codei ...

When integrating Stripe into my Next.js application, I encounter a build error

I encountered an error when building my Next.js app with Stripe integration using "yarn build". However, when running the app locally with "yarn dev", it works perfectly without any errors. I'm struggling to figure out why this discrepancy is happeni ...

"Encountering an issue where the route function in Passport and ExpressJS is not being

When using passport for admin authentication, I am facing an issue where the redirect is not calling my function. Consequently, all that gets printed on login is [object Object] Here is my code: app.get('/admin', isLoggedIn, Routes.admin); ...

TypeScript maintains the reference and preserves the equality of two objects

Retrieve the last element of an array, make changes to the object that received the value, but inadvertently modify the original last position as well, resulting in both objects being identical. const lunchVisit = plannedVisits[plannedVisits.length ...

Launching the Node.js application on Heroku resulted in encountering an issue: "Application error - There was a problem within the application

When I access the browser using http://localhost:8080/, I can see the message Hello World with Express. I am currently trying to deploy this application on Heroku. I have followed the tutorial provided by Heroku. 1) Create a new app 2) Choose an App name ...

Having trouble with accessing input field in a modal that was generated by TinyMCE React within a Next.JS environment

In my current project, I am utilizing Next.JS and looking to incorporate the TinyMCE editor onto my webpage. Here is the code snippet I have implemented: <TinyMceEditor selector='textarea' initialValue={ props.value } apiKey=<AP ...

How can Redux help persist input value through re-rendering?

Handling Input Value Persistence in Redux despite Re-rendering? I am currently able to store and save input values, but only the data from one step ago. For example, when I click on the second input field, it displays the value from the first input fiel ...

Dimensions of HTML Container on Google Website

I'm attempting to incorporate a collapsible table using HTML Box in a Google site. The code for the collapsible table can be found at http://tutorials.seowebpower.com/google-sites-advanced/collapsible-table. Here is the code: <html> <head> ...

Interconnected realms communication

I'm currently in the process of developing a Facebook iframe app. At one point, I initiate a friends dialog from Facebook and embed an HTML button to add some customized functionality for my app. dialog = FB.ui({ method:'fbml.di ...