简体   繁体   中英

Fastest way to truncate an object in JavaScript

I know that we can truncate an array like (not sure it is the best way!)

var person = ["John", "Doe", 50, "blue"]
person  = [];

now can you please let me know how I can truncate an object?

var person = {
    firstName:"John",
    lastName:"Doe",
    age:50,
    eyeColor:"blue"
};

Does some thing like this work?

var person = {
    firstName:"",
    lastName:"",
    age:,
    eyeColor:""
};

is there any better and faster way to do this?

What you posted isn't correct because you are simply assigning a new array to the variable that was storing the previous array... Doing that you lose the memory reference with the previous array...

var persons = ['Hitmands', 'You', 'ecc...'];
persons = []; // this is a new array not the previous array whitout values.

In many cases this is what you need but in cases where you need to keep the memory reference with the persons array... you can't!

So, a correct way to make an array empty without losing memory references is this:

var persons = ['Hitmands', 'You', 'ecc...'];
persons.length = 0;

Obtaining the same thing with an Object is a bit more difficult because Objects don't have any length (or similar) property (or method).

so, you can do something like following:

  1. Build a Custom Object that knows how to make itself empty

 var Person = (function() { function Person(name, job, hobbies) { this.name = name; this.job = job; this.hobbies = this.hobbies.concat(hobbies); } Person.prototype.name = ''; Person.prototype.job = ''; Person.prototype.hobbies = []; Person.reset = function(person) { for(var i in person) { if(!person.hasOwnProperty(i)) { continue; } person[i] = Person.prototype[i]; } }; return Person; })(); var hitmands = new Person('Hitmands', 'Frontend Developer', ['Javascript', 'Photography', 'Guitar', 'Rock']); console.log('hitmands is:', JSON.stringify(hitmands)); Person.reset(hitmands) console.log('now hitmands is:', JSON.stringify(hitmands)); 

Hope Helps

The fastest way would be:

person = { };

The problem with this solution is that all the parameters will be undefined .

There are some cases where undefined and null assume different meanings, mainly the former means a missed parameter while the latter indicates that the value of the parameter has been voluntarily set to null and this could make sense in some scenarios.

Moreover, the best approach also depends on your actual code and software architecture. As an example, if you have more than one reference to that object somewhere, you are not actually invalidating it and this way you will be going for sure towards annoying bugs.

That said, a possible alternative approach follows:

for(var prop in person) {
    if(person.hasOwnProperty(prop)) {
        person[prop] = null;
    }
}

This is neither the only one nor maybe the best one, but likely it's a good point from which to start.

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