0

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?

5
  • but I have to create the options again?! Commented Oct 31, 2015 at 16:41
  • Why? person.firstName will be undefined now, doesn't throw an error nor breaks your program if you call it. If you want to reassign values, you can just add them like before person.firstName = 'foo'; Commented Oct 31, 2015 at 16:42
  • so, we cant have something like NULL in JSON? Commented Oct 31, 2015 at 16:43
  • Don't get it. NULL? Why. What you have is an empty string in your reset version, not NULL, if you want to check if a property has a value, you can do so by if (person.firstName) which will be false either it's null or undefined Commented Oct 31, 2015 at 16:45
  • @Behseini: What exactly do you need this for? Commented Oct 31, 2015 at 16:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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.