0

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?

Juan
  • 61
  • 7
  • Don't confuse JSON (a textual data exchange notation) with native JavaScript arrays. What you are asking about has as much to do with JSON as cats have to do with lemons. Also, your `removeValue` seems work on array of objects, not arbitrary arrays. Given this, please elaborate on how exactly you want to add an item "by name / value". – Felix Kling Mar 22 '16 at 16:15
  • Apologies for improper use of terms and syntax. What I want is essentially the opposite of this (add instead of remove): http://stackoverflow.com/questions/6310623/remove-item-from-json-array-using-its-name-value – Juan Mar 22 '16 at 16:19
  • So you want `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. – Felix Kling Mar 22 '16 at 16:22
  • var users = [ {name: 'james', value: '1' }. ] I want to add a new name and value into the above variable. For example: name: 'thomas', value: '2'. Expected value would be users = [ {name: 'james', value: '1' }, {name: 'thomas', value: '2'} ] – Juan Mar 22 '16 at 16:27
  • Please [edit] your question to include this information. – Felix Kling Mar 22 '16 at 16:27
  • Edited to include this info – Juan Mar 22 '16 at 16:38

2 Answers2

0

With Array.prototype.push() ?

var sports = ["plongée", "baseball"];
var total = sports.push("football", "tennis");

console.log(sports); // ["plongée", "baseball", "football", "tennis"]
console.log(total);  // 4
bugyt
  • 556
  • 3
  • 11
0

I think a more fitting function is filter than map.

 Array.prototype.removeValue = function(name, value){
    var array = $.filter(this, function(v,i){
       return v[name] !== value;
    });
    this.length = 0; //clear original array
    this.push.apply(this, array); //push all elements except the one we want to delete
 }

I am only assuming the length and push hacks work, since I've never used them myself.

Katana314
  • 7,879
  • 2
  • 25
  • 34
  • This is not what the question is about. The OP wants to **add** something to the array. They only posted code to show that they know how to remove something (even though it's completely irrelevant). – Felix Kling Mar 22 '16 at 16:22