3

how to compare the key value (selected) in objects and remove the object in the main object. My object is:-

 mainObject={
       "2022-10-29":{
          "disabled":true,
          "disableTouchEvent":true
       },
       "2022-10-26":{
          "selected":true,
          "selectedColor":"#3634A3",
          "selectedTextColor":"#FFFFFF"
       },
       "2022-10-30":{
          "disabled":true,
          "disableTouchEvent":true
       },
       "2022-11-05":{
          "disabled":true,
          "disableTouchEvent":true
       }
    }

i want to remove the object

"2022-10-26":{
   "selected":true,
   "selectedColor":"#3634A3",
   "selectedTextColor":"#FFFFFF"
}

Final output should be:-

finalObject={
       "2022-10-29":{
          "disabled":true,
          "disableTouchEvent":true
       },
       "2022-10-30":{
          "disabled":true,
          "disableTouchEvent":true
       },
       "2022-11-05":{
          "disabled":true,
          "disableTouchEvent":true
       }
    }
harika
  • 49
  • 2
  • 1
    Does this answer your question? [How do I remove objects from a JavaScript associative array?](https://stackoverflow.com/questions/346021/how-do-i-remove-objects-from-a-javascript-associative-array) – wang Oct 25 '22 at 05:54

4 Answers4

5

You can remove a object's element using delete.

delete object[key];

In your code, like this.

delete mainObject['2022-10-26'];
ChanHyeok-Im
  • 541
  • 3
  • 11
  • Thank you @ChanHyeok-Im for response. But i want to delete the selected key element. The date will be changed every-time. Based on date we can't delete the object. Based on selected key we can delete the object. You can filter the selected one in above object! – harika Oct 25 '22 at 10:07
1

If you prefer some mechanical way, that you want to see the mechanism behind, it would be something like

const removeByKey = (object, key) => {
  const keyIndex = Object.keys(object).findIndex(objectKey => objectKey === key);

  if (keyIndex === -1) return object;

  const entities = Object.entries(object);
  entities.splice(keyIndex, 1);

  return Object.fromEntries(entities);
}

const reducedObject = removeByKey(mainObject, "2022-10-26");

Or else, there is a precise way in the language itself as @ChanHyeok-Im mentioned,

delete mainObject['2022-10-26'];
KGS Sandaruwan
  • 319
  • 2
  • 9
1

let mainObject = {
  "2022-10-29": {
    "disabled": true,
    "disableTouchEvent": true
  },
  "2022-10-26": {
    "selected": true,
    "selectedColor": "#3634A3",
    "selectedTextColor": "#FFFFFF"
  },
  "2022-10-30": {
    "disabled": true,
    "disableTouchEvent": true
  },
  "2022-11-05": {
    "disabled": true,
    "disableTouchEvent": true
  }
}

Object.values(mainObject).forEach((element, index) => {
  if (element.selected === true) delete mainObject[Object.keys(mainObject)[index]]
  //we loop through the values and select where selected is true, then we delete the object where the key index is the same index as the values. 

})

console.log(mainObject)
John Pachuau
  • 73
  • 1
  • 1
  • 6
0

Loop through objects and check if object has key selected and is true and delete the object

for (const key in mainObject) {
  if (mainObject[key].selected) {
    delete mainObject[key];
  }
}

console.log(mainObject);