Deleting Objects in JavaScript
The delete operator deletes only a reference, never an object itself. If it did delete the object itself, other remaining references would be dangling, like a C++ delete. (And accessing one of them would cause a crash. To make them all turn null would mean having extra work when deleting or extra memory for each object.)
Since Javascript is garbage collected, you don't need to delete objects themselves - they will be removed when there is no way to refer to them anymore.
It can be useful to delete references to an object if you are finished with them, because this gives the garbage collector more information about what is able to be reclaimed. If references remain to a large object, this can cause it to be unreclaimed - even if the rest of your program doesn't actually use that object.
How to delete an object in JavaScript?
The only way to delete an object is to remove all references for it.
The object created by your call to setBackgroundColor
is deleted almost immediately because you never keep a reference to it.
If you were to uncomment the line starting \\ var newObjet
then it would still be deleted almost immediately because the function would finish and the variable would drop out of scope.
I think that what you want to do is to remove the background colour setting your did.
That isn't part of the object. The function that did it was just attached to the object, but the change was to the element
you passed in, not to the object itself.
If you want to change the background colour to something else then you need to do so explicitly.
else {
document.querySelector('.my_container').style.backgroundColor = "";
That said, if you want to change the style of something based on the window size, use the media query feature built into CSS.
It is a lot less hassle than searching the DOM and modifying the style with JS.
How do I remove a property from a JavaScript object?
To remove a property from an object (mutating the object), you can do it like this:
delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];
Demo
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject);
How to delete object and all references?
No.
The only way to delete an object in JavaScript is to locate and delete each reference to it.
There's no generic mechanism to start with an object and automatically delete all references to it.
How to delete full object with javascript?
You can only use the delete operator to delete variables declared implicitly, but not those declared with var. You can set x = null or x = undefined
Deleting a row from a JavaScript object if it matches with all properties
Don't even specify the properties as arguments. You can define them as an object
itself.
This would also work
airport_data_1 = [
{ departure_time: "12:00", arrival_time: "03:00", city_id: "BOS" },
{ departure_time: "12:00", arrival_time: "03:00", city_id: "BOS" },
{ departure_time: "01:00", arrival_time: "04:00", city_id: "SFO" },
{ departure_time: "03:00", arrival_time: "05:00", city_id: "BOS" },
{ departure_time: "03:00", arrival_time: "05:00", city_id: "SFO" },
{ departure_time: "04:00", arrival_time: "06:00", city_id: "SJC" },
{ departure_time: "04:00", arrival_time: "06:00", city_id: "JFK" },
{ departure_time: "06:00", arrival_time: "09:00", city_id: "SJC" },
];
function remove_airport_row(arr, obj) {
return arr.filter((row) => {
// ingore the row if all the the properties matches to obj
return !Object.entries(obj).every(([key, value]) => row[key] === value);
})
}
console.log(remove_airport_row(airport_data_1, {
departure_time: "06:00",
arrival_time: "09:00",
city_id: "SJC",
}));
Deleting Objects in JavaScript. I'm a bit confused with JavaScript's delete operator
Looking at the definition of removeName, you're expected to send a person. You're currently not doing that, and you're placing the person inside the function too, and that would just end up scoping the person to this object.
So based on that info, this is how I assume you meant to write the code.
let user = {
name : "name",
surname: "surname"
};
function removeName (person){
delete person.name;
}
removeName(user);
console.log(user);
Deleting properties from an object of objects
I'd create two lists, one of the objects you want to delete from and one of the properties you want to delete and iterate over them in a nested loop:
const objects = ["pcc", "emr"];
const props = ["MedicalPractices", "CovidZone", "picturePath", "DoseSpotID", "consentStatus", "consentStatusLastUpdate", "consentStatusUpdatedBy", "consentStatusChangeReason", "syncStatus"];
objects.forEach(o => {
props.forEach(p =>
delete result[o][p];
});
});
What is the best way to delete an object in JavaScript?
You will have the best effect by doing this
window.obj = null;
delete window.obj;
Setting objects to null removes any references to it. Also remember that the delete command has no effect on regular variables, only properties.
To better ensure object destruction you may consider not using the global context at all, that is usually considered as an antipattern.
Related Topics
Babel 6 Regeneratorruntime Is Not Defined
Most Efficient Way to Create a Zero Filled JavaScript Array
Using Jquery to Test If an Input Has Focus
If a Dom Element Is Removed, Are Its Listeners Also Removed from Memory
Difference Between Domcontentloaded and Load Events
Difference Between Innertext, Innerhtml and Value
How to Connect to SQL Server Database from JavaScript in the Browser
How to Count String Occurrence in String
What Does 'Var That = This;' Mean in JavaScript
Can't Perform a React State Update on an Unmounted Component
How Does This JavaScript/Jquery Syntax Work: (Function( Window, Undefined ) { })(Window)
Definitive Way to Trigger Keypress Events With Jquery
How to Check Whether a Radio Button Is Selected With JavaScript
Set Keyboard Caret Position in HTML Textbox
State Not Updating When Using React State Hook Within Setinterval