0
help!!! how clear the all object?
help!!! how clear the all object? for example: var x = { x:1 y:2 } //need to completely clear the object, regardless of the names of the variables in the object. and get an empty object as output. how to do it?
7 Antworten
+ 3
var x = {
x:1,
y:2
}
console.log("before reset ") ;
for( let i in x )
console.log(i+" "+x[i]);
x={}
console.log("after reset") ;
for( let i in x )
console.log(i+" "+x[i]);
0
x = {}
0
Jayakrishna🇮🇳 does this really work? Do you have any evidence to support your answer to my question?
0
but does this method actually clear the object and not replace it?Jayakrishna🇮🇳
0
Not understood..
Your question is clear the object..
Replacing empty object is clear the contents in simple way..
0
Jayakrishna🇮🇳 are there harder ways? if there is, kindly provide them to me. Thank you
0
You can use 'delete' also
var x = {
x:1,
y:2
}
console.log("before reset ") ;
for( let i in x ) {
console.log(i+" "+x[i]);
delete x[i]
}
//x={}
console.log("after reset") ;
for( let i in x)
console.log(i+" "+x[i]);
// first one ({}) leaves a task for garbage collection but this deletes there itself..
hope it helps....