Moz Moz - 4 months ago 41
Javascript Question

JSON.stringify ignore some object members

Heres a simple example.

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

persons[0] = new Person();
persons[1] = new Person();

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!


I would create a new array:

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