I know how to remove an item from a json array, but I can't seem to make it work when adding.
The array:
var users = [
{name: 'james', id: '1'}
]
Want to add so it becomes:
var users = [
{name: 'james', id: '1'},
{name: 'thomas', id: '2'}
]
Here's the code for removing an array:
Array.prototype.removeValue = function(name, value){
var array = $.map(this, function(v,i){
return v[name] === value ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}
removeValue('name', value);
//space removed
What changes would I need to make to reverse to add values to the array?
removeValueseems work on array of objects, not arbitrary arrays. Given this, please elaborate on how exactly you want to add an item "by name / value".addValue('name', value);to what exactly? Add a new object to the array? Update all objects in the array? If would just provide an example of input and expected output, it would be much easier for us to help you. The code you posted is pretty much irrelevant to your question.