How to Add and remove items from a json easily?

Asked

Viewed 10,174 times

1

I have a json similar to this

[{id: 1, titulo: 'compra', valor: 50.00, data:'2014-10-23' },{id: 1, titulo: 'compra', valor: 60.00, data:'2014-10-24' } ]

I have a function to add, which adds a new item and one to remove. The add function is ok, but I would like to know how to remove an item.

There can be several items the same but on different dates, that is, several items with the same ID only with different dates.

  • 1

    Looking at this question and the previous one, I realized that you seem to misunderstand what JSON is. It is a format for data exchange, serialized as string. It is not meant to be manipulated without first being deserialized (which all the answers here and in the other question consider is already done). It may seem like a silly distinction, but it’s important to know.

  • I get it. I’m using it to leave various information in memory to manipulate after the end of a process... I believe that what I want to do can be done in another way. But I appreciate the explanation, it was very useful.

  • What you want to do is correct, I just wanted to make it clear that at this point you no longer operate on JSON, but on normal JS objects and arrays.

  • I understand perfectly. Thank you very much!

  • my friend... shows the function to add new obj in json!! please

3 answers

2

If you want to remove all elements from the array with a certain ID you can do so:

function removerID(id, arr) {
    return arr.map(function (obj) {
        if (obj.id != id) return obj;
        else return false;
    }).filter(Boolean);
}

jsFiddle: http://jsfiddle.net/s4ypk2be/

In this case I suggested you do not change the initial array. You can always do arr = removerID(1, arr);, but if you want to change the array even without passing it to the function you can do the assignment inside the function:

function removerID(id) {
    arr = arr.map(function (obj) {
        if (obj.id != id) return obj;
        else return false;
    }).filter(Boolean);
}

Another alternative is to make a map with the index of the objects that have a certain ID but that only makes the code more complex and I doubt that much improves the performance... qq would be something like this:

function removerID(id) {
    arr.map(function (obj, i) {
        if (obj.id == id) return i;
        else return 'foo';
    }).reverse().map(function (i) { // uso o reverse para ele usar indexes decrescentes
        if (typeof i == 'number') arr.splice(i, 1);
    })
}
removerID(1);

jsFiddle: http://jsfiddle.net/s4ypk2be/2/

1


The function below removes the JSON input by any key.

It’ll fit you like a glove! ;-) (demo)

var meuJSON = [{id: 1, titulo: 'compra', valor: 50.00, data:'2014-10-23' },{id: 1, titulo: 'compra', valor: 60.00, data:'2014-10-24' } ];

function removerPela(chave, valor){
    meuJSON = meuJSON.filter(function(jsonObject) {
        return jsonObject[chave] != valor;
    });
    return meuJSON
}

console.log(removerPela("data","2014-10-23"));
  • In case it would be by ID and Data... would have to filter by both. What would it be like? Because on the same date can have other values that do not need to be deleted.

0

You can do this using the function delete. Take a look here. Tip: an id is a unique identifier. Do not use the same id for more than one different element.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.