Search code examples
javascriptjqueryjsonstringify

JSON.stringify ignore some object members


Heres a simple example.

function Person() {
  this.name = "Ted";
  this.age = 5;
}

persons[0] = new Person();
persons[1] = new Person();
JSON.stringify(persons);

If I have an array of Person objects, and I want to stringify them. How can I return JSON with only the name variable.

The reason for this is, I have large objects with recursive references that are causing problems. And I want to remove the recursive variables and others from the stringify process.

Thanks for any help!


Solution

  • I would create a new array:

    var personNames = $.map(persons,function(person){
      return person.name;
    });
    var jsonStr = JSON.stringify(personNames);