简体   繁体   中英

Remove objects from an Array based on values in another array

I am trying to create an array with 7 objects (each per day). Idea is to create store hours for 7 days.

Final Dictionary will be like this:

    const storeHours = {
      monday: { startTime: mondayTime1, endTime: mondayTime1 },
      tuesday: { startTime: new Date(), endTime: new Date() },
      wednesday: { startTime: new Date(), endTime: new Date() },
//  other days ...
    };

I have 7 checkboxes, one for each day.

When I click checkbox, it stores checked item in an array.

checkedValues = ["Friday", "Saturday"] 

I can use this checkedValues items to know which entry to make in newDict.

like this:

newDict = {
monday: { startTime: mondayTime1, endTime: mondayTime1 },
}

When I click second checkbox, newDict should have new entry for that day and so on.

When I un-check the checkbox I have to also remove the entry from this dictionary, so both, checkbox and dictionary, remain in sync.

how do I remove an entry from dictionary when an item is un-checked?

Whole idea is to basically store store hours in database with dictionary with 7 objects..

I am not sure what you are trying to achieve here, please edit your question to make it more clear. So, as far as I can understand, here is a possible solution.

1. Store your entries in a dictionary

As your code is right now, finalArray is an array containing a single dictionary that you use to store entries. That is inefficient, just use the dictionary:

finalDict = {
  Friday: {startTime: startTime}, 
  Saturday: {endTime: endTime}
}

And since it is not an array, but a dictionary, I have renamed it accordingly to avoid confusion.

2. Set the entry to null

The cool thing of using dictionaries, is that you can reference an entry by its name. So, if Saturday is marked, then you need to simple:

finalDict.Saturday = null;

Or:

finalDict["Saturday"] = null;

After you do that, your dictionary will have the following value:

{
  Friday: {startTime: startTime}, 
  Saturday: null
}

Assuming you still have the array with checkedValues you can use .filter() and then .reduce() to leave only the days from checkedValues . Also, you can use Object.keys() which will return the names of storeHours 's keys.

 const checkedValues = ["monday", "tuesday"]; const storeHours = { monday: { startTime: 1, endTime: 3 }, tuesday: { startTime: new Date(), endTime: new Date() }, wednesday: { startTime: new Date(), endTime: new Date() }, // other days... }; const newDict = Object.keys(storeHours).filter(day => checkedValues.includes(day)).reduce((obj, key) => { obj[key] = storeHours[key]; return obj; }, {});; console.log(newDict);

This returns:

{
  "monday": {
    "startTime": 1,
    "endTime": 3
  },
  "tuesday": {
    "startTime": "2020-07-27T06:50:27.858Z",
    "endTime": "2020-07-27T06:50:27.858Z"
  }
}

More on the subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM