Filter an object within an array based on the index values of another array

Imagine a situation where users can select multiple items and remove them. This calls for two arrays:

  1. One containing checkbox values (checked status and index)
  2. The other holding the actual items that need to be filtered based on the checked values' index.

Here are the two arrays and the expected result using lodash:

const checked = [
  {
    index: 0,
    checked: false
  },
  {
    index: 1,
    checked: true // note that the second index is checked so we need to filter out the second item from the items array.
  },
];

const items = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  },
  {
    title: 'This is title 2',
    description: 'This is description 2',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];


const result = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];

Answer №1

To solve this, simply utilize the filter function to retrieve the index of the current object. Then, using this index, access the n-th item from the checked array (I'm suggesting this approach as it's evident from the checked array that your array contains states for all checkboxes - both checked and unchecked) and verify its checked property.

const checked = [
  { index: 0, checked: false },
  { index: 1, checked: true }
];

const items = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  },
  {
    title: 'This is title 2',
    description: 'This is description 2',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];

const filtered = items.filter((item, index) => !checked[index].checked);

console.log(filtered);

Answer №2

Just follow these steps.

let output=[];
selectedItems.forEach(function (element) {
    if(element.selected)
    {
        output.push(selectedElements[element.index]);
    }
})

console.log(output);

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

There appears to be a malfunction with WebRupee

I have incorporated the new rupee sign into my website to represent Indian currency. Below is the code snippet I used: For the script, I included " and also added the following: <span class="WebRupee">Rs.</span> 116,754.00 To style it using ...

What steps can be taken to troubleshoot the error message "InjectionToken angularfire2.app.options! not found" during testing of Firestore in Angular?

During my Angular test, I encountered an issue with a component and a service that utilizes Firestore. Here is the error message from my ItemService: NullInjectorError: R3InjectorError(DynamicTestModule)[ItemService -> AngularFirestore -> InjectionTo ...

Adding Floating Point Numbers in TypeScript Objects

Recently, I've been encountering some strange results while trying to sum up my floating point values. The output seems off with 8 decimal places and other unexpected outcomes. All I really want to do is add up the protein values of various objects to ...

Establishing a connection to a remote Mongo database using Node.js

I have a remote MongoDB server and I am looking to establish a connection with it using Node.js. I was able to successfully connect directly with the Mongo shell, but for some reason, I cannot establish a connection to the MongoDB server in Node.js. The co ...

Step-by-step guide on making a post request to the Facebook Graph Api with Httparty in a Rails application

I'm currently working on developing a bot for Facebook Messenger, and I need to make a post request call to the Facebook Graph API. The sample code provided by Facebook is in Node.js, but I am working with Rails as my backend framework. Sample Code ...

How can I use TypeScript to wrap a component in Vue 3?

Looking to customize a PrimeVue component (Calendar) by styling it differently and then re-exporting it. Here's an example in React: const WrappedCalendar: React.FC<CalendarProps> = (props)=> <div style={{background:'green'}}&g ...

How to manage multiple sockets within a single thread using ZeroMQ.js

Currently, I am in the process of setting up a service using ZeroMQ in conjunction with Node.js. Utilizing the ZeroMQ.js package for this purpose. Reference can be found in The ZeroMQ Guide, which outlines how to manage multiple sockets within a single ...

Creating a webpage using webkit technology

As someone new to web development, I am eager to create a website that is user-friendly on both desktops and mobile devices. Recently, I stumbled upon a site with impeccable design and functionality using what appeared to be "screen webkit". I'm curi ...

Error SCRIPT438: The function 'formatCurrency' is not supported by this object

I'm encountering an "SCRIPT438: Object doesn't support property or method 'formatCurrency'"" error when attempting to format currency for cells within a jQuery datatable using the jQuery formatCurrency library. Below is the code snippe ...

Deactivate the button when submitting the form

Is there a way to disable a button on a form submission in order to prevent users from submitting multiple times? I attempted to disable the button with JavaScript using onclick, but encountered an issue where the button remained disabled if client side v ...

Contrasting createMuiTheme and getMuiTheme

When would you choose to use one over the other? What are the key differences in how they operate? ...

Implementing real-time streaming communication between server and client with Node.js Express

When a post request is made, the server generates data every few seconds, ranging from 1000 to 10000 entries. Currently, I am saving this data into a CSV file using createWriteStream and it works well. How can I pass this real-time (Name and Age) data to t ...

JavaScript Cursor-Based Auto Typing Tool

I want to develop a straightforward Javascript auto writer within an HTML page. Below is the code I have written: <html> <head> <style> #type { display: inline-block; } #cur { display: inline-block; ...

What is the best way to send the selected option from a dropdown to a button click function within the controller?

I need to pass the selected value from a user's country selection dropdown to the ng-click="saveChanges()" function on the submit button. Is there a correct method for achieving this? I want to be able to access the user's selection from the dro ...

initiating a function on a separate webpage using ajax

Our website includes an IIFE script from a third party to check for a specific URL parameter and set a cookie. However, on another page, we need to set this parameter without redirecting the user, reloading the page, or altering the third-party code. The ...

Execute function periodically using Vue.js

I have a requirement to update data periodically by calling a function in my Vue.js application. I am looking to call the 'listar()' method every 30 seconds and store the response in a property of vue js. If anyone can guide me on where to locat ...

Transmitting information via Ajax, jquery, Node.js, and Express

Seeking assistance as I struggle to comprehend the process while trying to implement it based on various online resources. My carousel directs users right after signing up, and I aim to gather information about their profile through simple input questions. ...

PHP: Link to logo in different folder by including 'nav.php'

I am facing an issue with my nav.php file: <div> <!-- there's a lot of code here so I want to write it once and include it in all pages within the main folder as well as subfolders <img src="logo.png"> </div> The structur ...

The issue of variable being displayed as undefined is often due to JavaScript hoisting mechanisms

UPDATE: After diagnosing the issue, I discovered that the #t01 ID is inaccessible within the ajax success function which is why nothing is being displayed. This is related to a concept known as JavaScript hoisting. Unfortunately, I couldn't find a sol ...

Securing your forms with reCaptcha and validation techniques

Greetings, I am trying to integrate reCaptcha(2) into my Register form. While I have successfully imported it, the validation process seems to be ineffective. Upon clicking submit, the form skips over the empty reCaptcha without prompting for completion. C ...