I have an array of javascript objects that represent users, for example:
[
{ userName: "Michael",
city: "Boston"
},
{ userName: "Thomas",
state: "California",
phone: "555-5555"
},
{ userName: "Kathrine",
phone: "444-4444"
}
]
Some objects contain some properties, but not others. I need a clean way to ensure that ALL objects get the same properties. If they do not exist, I want them to have an empty string value, for example:
[
{ userName: "Michael",
city: "Boston",
state: "",
phone: ""
},
{ userName: "Thomas",
city: "",
state: "California",
phone: "555-5555"
},
{ userName: "Kathrine",
city: "",
state: "",
phone: "444-4444"
}
]
Update
I should have been more specific. I was looking for an option that would handle this situation dynamically, so I do not need to know the properties in advance.
For a specific jQuery, the parameter $.extend()is good, but will only work if you know ALL properties ahead of time.
, , , , , , , :
1) JSON, , 900 1000 1 9 .
2) "" , JS, , , .