Remove elements from the first array that are not present in the second array

Resolved: I now understand that each filter requires an explicit return statement. I mistakenly thought the single boolean in each filter would suffice. by @adiga

I am trying to identify elements in one array (dcm) that do not appear in a second array (vari). Specifically, I am looking for matches based on two elements - vp (string type) and vd (date type). While I have ensured there are rows in dcm that meet this criteria, I am not getting any results.

Could I have made a mistake in my code? Is there a better way to achieve this using methods like .includes, .contains or .indexOf?

  var dcmm = dcm.filter(r=>{
    vari.filter(rv=>{
      rv[vp]+rv[vd] == r[dp]+r[dd]
      }).length == 0
    });

ps. Apologies to those who prefer shorter variable names and advocates of using const instead of var. pps. Although this is Google Apps Script and not JavaScript, I believe the concept remains the same.

Answer №1

Just a reminder, mentioned by @adiga, that using curly braces for code blocks means you don't necessarily need to include the return statement.

This approach should work effectively:

var dcmm = dcm.filter( r => vari.filter( rv => (rv[vp]+rv[vd] == r[dp]+r[dd]) ).length == 0 );

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

Exploring Complex Parent-Child Connections with FeathersJS

Is there an optimal method for managing deep nested parent-child relationships in Feathers using the recommended Association Method? Let's say our data entries look like this: [ {"name": "Grandpa", "id": 1, "parent": null}, {"name": "Mom", " ...

What is the JavaScript mouseenter/out event all about?

My goal is to create a hover effect using JavaScript. I am utilizing the mouseenter and mouseout events to display the content when the mouse hovers over a button and remove the content when the mouse moves out. However, I am facing an issue where the cont ...

Is there anything suitable to keep in SharedPreferences?

I'm working on a file explorer to sync an FTP directory on my phone. I want to know which files are new after each synchronization, even if the app is closed and reopened in the next session. My idea is to save an array of new files and compare it in ...

JavaScript and JSON interchangeably, the first AJAX response should be rewritten by the second response

I am facing an issue with my code: I have two ajax calls being made on window.load, and it seems like the response from the second AJAX call is overwriting the response from the first one before my function can process it. I'm not sure where I'm ...

Creating PopUp Windows with PHP and JavaScript

There is a function created on my page that opens a pop-up window when clicking on a game-mod name: <SCRIPT language="javascript" type="text/javascript"> function popModData( modName ) { var url = "./modList.php?mod=" + modName; ...

What is the best method for retrieving information from MongoDB and presenting it in a table with Node.js?

I'm struggling to understand how to retrieve data from mongodb and display it in an html/ejs file. I have a button in the html/ejs file that, when clicked, should show all the data from a mongodb database collection. While I've come across simil ...

injecting javascript dynamically using jquery

I am attempting to conditionally load a script in the case that the browser being used is IE8. To achieve this, I have employed jQuery's .getScript function as it allows me to execute certain actions after the script has been loaded. The issue lies in ...

A guide on conducting comparisons within a render in React

Currently, I am placing a component inside another component and the setup is shown below: render() { return ( <MyComponent value={this.state.measurement.Value} unit="kg" /> ); } My objective is to m ...

Allow Vue to handle the registration of the datepicker event

Is there a way to notify Vue when the datepicker changes the selected date? new Vue({ el: '#app', data: { termin: '' }, computed: { }, methods: { } }) This is just an input field: <div id="app"> <div c ...

Should I use YUICompressor or a comparable tool in PHP?

After using yuicompressor.jar for quick JavaScript minimisation on my test server, I ran into issues when deploying to my public server due to server restrictions on java execution. This means I need to find an alternative solution for on-the-fly JS compre ...

Having trouble decoding JWE using the NPM Jose library

Although I can successfully decrypt a JWE using an older version of jose, I'm facing difficulties in utilizing the latest version API. The headers of my token are as follows: { "alg": "A128KW", "enc": "A128CBC-H ...

Is there a way to update the content of both spans within a button component that is styled using styled-components in React?

I am interested in creating a unique button component using HTML code like the following: <button class="button_57" role="button"> <span class="text">Button</span> <span>Alternate text</span> ...

Explore the category options in the reference document using Node.js

Category Schema const CategorySchema = mongoose.Schema( { catName: { type: String, required: true } }, { timestamps: true } ); Product Schema const ProductSchema = new mongoose.Schema( { brand: { type: String, required: true }, title: ...

The permanent header is covering up the vertical scroll bar

Currently, I am attempting to integrate a jquery plugin from this source - https://gist.github.com/3819758, into a table in order to create a fixed header. However, the table in my case is too wide, causing the generated header to obscure the vertical scro ...

Integrate UploadFS functionality into angular2-meteor

Currently, I am in need of a file storage database and after some research, it appears that UploadFS is the most suitable option for my project. My development involves Angular2 typescript and Meteor. meteor add jalik:ufs-gridfs However, I am encounterin ...

Is there a method to compress all of my JS/CSS files before displaying them in the browser without doing so while editing them in the IDE?

I have a JavaScript file with numerous comments and unnecessary spaces, making it larger and slowing down the website. I am seeking a solution to minify the file when starting the server, while still being able to edit the original file in my IDE. Essen ...

Interacting with the header component within the renderHeader property of the MUI Data Grid Pro will update the sortModel

Currently, I am utilizing the Material UI DataGridPro component to construct a React Js application. I am interested in developing a customized filtering feature. In the image below, you can see a red box representing an IconButton for the filtering view ...

Is it necessary for React DOM to utilize className? I find it peculiar that I'm not receiving any alerts or warnings

I've been experimenting with React in a project for several weeks now. It just crossed my mind that I should be using className instead of class in regular DOM and SVG elements, as class is a reserved keyword in JavaScript. Despite this realization, ...

Applying Media Queries Based on Element Height

Situation: In my scenario, there is an image with two buttons below it, all of which share the same width. Issue: When the viewport is too wide horizontally, the buttons are not visible without scrolling down. This poses a challenge for tablets and small ...

Setting the expiry time for vue-cookie in hours Would you like to learn

I'm currently using the following code to set a 3-hour expiry for my vue-cookie: VueCookie.set('S3ID', getS3ID, 3); However, it seems that this function is actually setting the cookie expiry time as 3 days instead of 3 hours. Can anyone ex ...