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
       }
    }
1

4 Answers 4

5

You can remove a object's element using delete.

delete object[key];

In your code, like this.

delete mainObject['2022-10-26'];
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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'];

Comments

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)

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.