The use of set.has in filtering does not produce the desired outcome

I am attempting to use Set.has to filter an array in the following way:

const input = [ 
  { nick: 'Some name', x: 19, y: 24, grp: 4, id: '19340' },
  { nick: 'Some name', x: 20, y: 27, grp: 11, id: '19343' },
  { nick: 'Some name', x: 22, y: 27, grp: 11, id: '19344' },
  { nick: 'Some name', x: 22, y: 30, grp: 11, id: '19350' },
  { nick: 'Some name', x: 22, y: 12, grp: 23, id: '19374' },
  { nick: 'Some name', x: 22, y: 29, grp: 23, id: '19377' } 
];

const grpToOmit = [ 11, 23 ];
const groupToOmitSet = new Set(grpToOmit);

input.filter(it => {
  console.log(groupToOmitSet.has(it.grp))
  return !groupToOmitSet.has(it.grp);
});

console.log(input)

After creating a collection of unique values from the grpToOmit array, I check for them within the filter function.

Despite getting multiple true results in

console.log(groupToOmitSet.has(it.grp))
, this filter does not produce any changes to the input array.

Answer №1

.filter creates a fresh array, rather than modifying the current one. Make sure to store the result in a variable.

const data = [ 
    { nick: 'Some name', x: 19, y: 24, grp: 4, id: '19340' },
    { nick: 'Some name', x: 20, y: 27, grp: 11, id: '19343' },
    { nick: 'Some name', x: 22, y: 27, grp: 11, id: '19344' },
    { nick: 'Some name', x: 22, y: 30, grp: 11, id: '19350' },
    { nick: 'Some name', x: 22, y: 12, grp: 23, id: '19374' },
    { nick: 'Some name', x: 22, y: 29, grp: 23, id: '19377' } 
];

const groupsToExclude = [ 11, 23 ];
const excludedGroupsSet = new Set(groupsToExclude);

const result = data.filter(item => {
  return !excludedGroupsSet.has(item.grp);
});

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

Having trouble installing the @mui/x-data-grid package in a React project

npm install @mui/x-data-grid encounters a problem that throws an error message: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Utilizing Next.js - Implementation of a unique useThemeMode hook to efficiently manage theme preferences stored in the browser's localStorage, seamlessly integrating with toggleTheme

For a while now, I've been trying to figure out how to toggle my favicon based on the theme logic of my application and the current theme state stored in localStorage. My approach involves using CSS variables and data attributes applied to the html bo ...

Tips for avoiding problems with quoting and using apostrophes in a JavaScript function inside a tag in a JSP file

Within my JSP, I have a string value stored in ${state.status.code} that I need to pass to a JavaScript function when a table element is clicked using onClick to trigger the showStatus function. Here is how I have attempted to achieve this: <c:set var= ...

Gather input from a form and store it in an Object upon submission

I am facing a challenge as I do not have much experience with object-oriented JavaScript. My goal is to have a form submit details such as the first and last name to an object that I've created. Here is the object I have: function Person(firstName, ...

Having issues with Vue.js and the splice method not functioning properly on an array row

Having an Object array, I noticed that when I try to remove an object from the array list, only items are deleted from the end. <div class="hours" v-for="(time, index) in hour" :key="index"> So, I decided to place a cli ...

Utilizing string interpolation within the parameters of an AJAX call

I have a locale variable that can hold values such as en, fr, or es. The goal is to include this locale key in an AJAX request as part of the data parameter. let locale = "en"; let name = "example"; $.ajax({ url: "/path", method: "PUT", data: {chara ...

What's preventing me from using the left click function on my published blog post?

This is my first time creating a blogger template and everything seems to be working correctly. However, I have encountered one problem. For some reason, I am unable to left click on my blog post. I have not installed any right/left click disabler and I&a ...

Easily integrating a JavaScript file into an HTML document when utilizing a NodeJS Express server

Currently in the process of developing a chat application, utilizing the Express server with NodeJS and AngularJS for client-side management. Encountering an issue when attempting to include /js/code.js in my html, as it cannot be found due to not being p ...

How to detect internet connection in any screen using React Native?

I'm facing confusion regarding how to display my customDialog when there is no Internet connection in my app. Currently, I have successfully shown my customDialog only in the LoginScreen. However, I want to display it from different screens, not just ...

Leveraging JSON Data in Highcharts

I am looking to create a column range chart using Highcharts to display a series of high and low temperatures. I found a demo example that showcases the kind of chart I want on the Highcharts website at: The data I have is stored in a file named datatest. ...

Is there a way to display errors during jQuery validation?

Is there a specific function that can provide all error messages generated during form validation? I attempted to use the defaultshowerrors() function, but it only shows errors for the current element being validated. Is there a way to retrieve all error ...

What are the proper ways to implement JavaScript conditions?

Having an issue with my slider function, as it currently only works once with the moveRight() method. To address this problem, I attempted to implement a condition that disables the move function and modifies the attributes of links on the second click. H ...

AJAX throws an error when a function is used with a variable

My knowledge of jQuery and Javascript is quite limited, and I prefer not to work with them extensively. However, I always encounter an error that I can't seem to troubleshoot. Below is my code for ajax / javascript: function eintragen(id){ $.post( ...

Determine which JavaScript script to include based on whether the code is being executed within a Chrome extension

I am in the process of developing a Chrome extension as well as a web JavaScript application. I currently have an HTML container. I need the container.html file to include <script src="extension.js"> when it is running in the Chrome extension, and ...

The Next.js application is functioning smoothly in development, but encounters errors during the building and deployment processes

While my Next.js app compiles and runs smoothly locally during development (using npm run dev), I encounter a failed build when attempting to compile the project (using npm run build). After researching online, it seems that unhandled promises may be the c ...

JavaScript - All values stored from the for loop are registering as undefined

Recently delving into the realm of JavaScript programming, I find myself faced with a new challenge. While not my first language, this is one of my initial ventures with it. My current project involves creating a chess program utilizing the HTML5 canvas fe ...

Transform an array of objects into a nested tree structure with Javascript

I am currently facing a challenge with handling a complex json file using javascript to structure it hierarchically. My goal is to convert an array of objects into a deeply nested array, where there can be multiple divNames with varying categories and subc ...

Warning: The DataTables alert has been triggered for table ID DimStatus. It is indicating that an unknown parameter, 'Code', has been requested for row 0 and column 0 during the initialization

https://i.stack.imgur.com/yEpSp.pngI am encountering an error while attempting to display my data in a datatable. The structure of my table is as follows: [Table("DimStatus", Schema = "dmg")] public class PolicyState { [Key] ...

Use two separate AJAX requests to open and close the modal dialog

I am experiencing an issue with my subscription form that uses modal windows. After closing the window and trying to subscribe again, the ajax calls are being duplicated, resulting in an error. $("#openModal").click(function() { if($("#wname").val() = ...

What is the best way to wrap a call to XMLHttp Request within a $q promise?

I am trying to figure out how to wrap the following code in an AngularJS ui-router resolve section with a $q promise. The goal is to return success or fail based on whether the http request works. Should I encapsulate all of this inside a promise or just ...