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?

6
  • 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". Commented Mar 22, 2016 at 16:15
  • Apologies for improper use of terms and syntax. What I want is essentially the opposite of this (add instead of remove): stackoverflow.com/questions/6310623/… Commented Mar 22, 2016 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. Commented Mar 22, 2016 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'} ] Commented Mar 22, 2016 at 16:27
  • Please edit your question to include this information. Commented Mar 22, 2016 at 16:27

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.