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

Navigating websites: Clicking buttons and analyzing content

When looking to navigate a website's directory by utilizing buttons on the page or calling the associated functions directly, what language or environment is most suitable for this task? After experimenting with Python, Java, Selenium, and JavaScript, ...

Utilizing Express.js: A Guide to Fetching File Downloads with a POST Method

Although GET requests are successful, I am facing challenges when using POST to achieve the same results. Below are the different code snippets I have attempted: 1. app.post("/download", function (req, res) { res.download("./path"); }); 2. app.post ...

ng-repeat not functioning properly when using track by, filter, and orderBy

I have come across this code snippet. http://jsfiddle.net/0tgL7u6e/ Scripting in JavaScript var myApp = angular.module('myApp',[]); function MyCtrl($scope) { $scope.nameFilter = ''; $scope.contacts = [ {name: 'G ...

What is the best way to transfer an array from an Express Server to an AJAX response?

My AJAX request successfully communicates with the server and receives a response that looks like this: [{name: 'example1'}, {name: 'example2'}] The issue arises when the response is passed to the client-side JavaScript code - it is t ...

The input form in my Node.js project is not adapting to different screen sizes. I am currently utilizing ejs as the template

I have integrated ejs as a template in my Node.js project, but I am encountering an issue with the input form in the following code snippet. The form is unresponsive, preventing me from entering text or clicking on any buttons. What could be causing this ...

Tips for maintaining the particles.js interaction while ensuring it stays under other elements

I have incorporated particle.js as a background element. By adjusting the z-index, I successfully positioned it in the background. While exploring solutions on the Github issues page, I came across a suggestion involving the z-index and this code snippet: ...

Guide to attaching click and keydown events to an input field using Vanilla JavaScript

I am currently working on a project for freecodecamp where I have successfully created a functional example. However, there are a few things that I'm struggling to figure out. Firstly, I need help with executing an AJAX request by either typing in th ...

Implementing ESM in your next.config.js file is critical for optimizing

Currently, I am in the process of optimizing a Next.js project and came across the requirement to include type: 'module' in thepackage.json file. However, this led to an error being thrown: Error [ERR_REQUIRE_ESM]: Must use import to load ES Mo ...

Use JavaScript to sift through an array and exclusively retrieve items that match a specific value

I am working with an array of objects that contain a phase key, and I want to filter out only the ones that have a specific phase value. Additionally, I need to map some other key/value pairs into the final return. Here is my current code: phaseToBlocks ( ...

Is there a way to retrieve a collection of files with a particular file extension by utilizing node.js?

The fs package in Node.js provides a variety of methods for listing directories: fs.readdir(path, [callback]) - This asynchronous method reads the contents of a directory. The callback function receives two arguments (err, files), with files being an arra ...

What is the best way to fully eliminate the Pixi renderer, stage, and all associated assets

I am currently faced with a challenge of mounting and unmounting a Pixi stage using React without relying on react-pixi. Upon re-mounting the component, I encounter the following error: Uncaught Error: Resource with name "cupCake.png" already exists i.ad ...

Struggling to render images within a child component in a React application when passed as a prop from the parent component

Currently immersed in a React project, here is the structured layout of my work: public index.html src App.js images facebook.png image.PNG linkedin.png profile.png twitter.png Profile Intro profileIntro.js data data.js Within App.js, I import ...

What is the most efficient way to query through a Firestore database containing 5,000 users?

We are currently facing a challenge with our staffing application, which is built using vuejs and a firestore database containing over 5,000 users. Our primary issue lies in the need for a more efficient layout that allows admins to search for users within ...

Enhancing OpenSeadragon images with custom overlays and managing error messages

Currently, I am experimenting with the Basic Single-Row Tile Source Collection example using the same configurations and tile sources outlined in the example. However, I am encountering difficulties in adding overlays to the first and second images as int ...

Is there a maximum size limit for the Fabric.js Path Array?

Has anyone tried plotting a line graph using Fabric.js and encountered issues with the fabric.Path? I've noticed that it stops drawing after 8 segments even though I have attempted different methods like loops and individually coding each segment. co ...

When the user clicks, I plan to switch the audio source

I am looking to update the audio source when a button is clicked, but I am having trouble getting it to work. image description data() { return { audioSrc: '' } }, methods: { setActiveAudio(item) { this.$refs.audioE ...

What is the best way to terminate a "for await ...of" loop if it fails to finish within a specified timeframe?

Is it possible to stop an asynchronous loop if it doesn't finish within a set time frame? I'm working with the following code: (async()=>{ for await(let t of asynDataStreamOrGenerator){ //some data processing } //some other code I n ...

The time.js library is providing inaccurate second values for the given date and time

Utilizing the moment.js library along with the Timezone and BusinessDays extensions in Vue.js, I am developing a datetime format for storing in a MySQL database. Here is the code snippet: import moment from 'moment-timezone' ...

Creating a dynamic pulse effect using jQuery to manipulate CSS box-shadow

My goal is to create a pulsating effect using CSS box-shadow through jQuery. Despite my best efforts, the code I attempted failed to produce the desired smooth pulse effect with box-shadow. The code snippet I experimented with: <div class="one"> &l ...

What steps can be taken to resolve an error encountered when attempting a dynamic data POST request from the body

Whenever I attempt the post method to fetch data on Postman and store it in a local MongoDB database, I encounter an error. The error message indicates a bad request with a status code of 400. *The following is app.js: var express = require('express& ...