2010-08-10 2 views
0

J'ai cette structure json et je l'ai fait dans un tableau. J'ai essayé de supprimer l'entrée, mais ce code a échoué sur moi: Remove item from array if it exists in a 'disallowed words' arraycomment supprimer un élément dans un tableau javascript avec la forme json

var historyList = []; // assuming this already has the array filled 

function add() { 
    var newHistory = { 
       ID: randomString(), 
       Name: $('#txtVaccineName').val(), 
       DoseDate: doseDate, 
       ResultDate: resultDate, 
       Notes: $('#txtResultNotes').val() 
      }; 
    historyList.push(newHistory); 
}; 

function removeEntry(value) { 
    historyList.remove('ID', value); 
}; 

Array.prototype.remove = function(name, value) { 
      array = this; 
      var rest = $.grep(this, function(item) { 
       return (item[name] != value); 
      }); 

      array.length = rest.length; 
      $.each(rest, function(n, obj) { 
       array[n] = obj; 
      }); 
     }; 
+0

Remarque: vous utilisez un littéral d'objet, pas JSON. JSON est quelque chose de différent. Voir: http://www.json.org/ – strager

Répondre

0

Vous pouvez utiliser un filtre de propriété pour correspondre à l'élément dans votre liste d'historique. Ci-dessous l'exemple de code rapide pour le faire, j'ai modifié un peu votre historique.

Un test rapide en FF montre que cela fonctionne!

var historyList = []; // assuming this already has the array filled 

function addToHistory(name, notes) { 
    var newHistory = { 
      ID: new Date().getTime(), 
      Name: name, 
      DoseDate: "Somedate", 
      ResultDate: "SomeResultDate", 
      Notes: notes, 
      toString: function() {return this.Name;} 
    }; 
    historyList.push(newHistory); 
}; 

var Util = {}; 

/** 
* Gets the index if first matching item in an array, whose 
* property (specified by name) has the value specified by "value" 
* @return index of first matching item or false otherwise 
*/ 
Util.arrayIndexOf = function(arr, filter) { 
    for(var i = 0, len = arr.length; i < len; i++) { 
     if(filter(arr[i])) { 
     return i; 
     } 
    } 
    return -1; 
}; 

/** 
* Matches if the property specified by pName in the given item, 
* has a value specified by pValue 
* @return true if there is a match, false otherwise 
*/ 
var propertyMatchFilter = function(pName, pValue) { 
    return function(item) { 
     if(item[pName] === pValue) { 
     return true; 
     } 
     return false; 
    } 
} 


/** 
* Remove from history any item whose property pName has a value 
* pValue 
*/ 
var removeHistory = function(pName, pValue) { 
    var nameFilter = propertyMatchFilter(pName, pValue); 
    var idx = Util.arrayIndexOf(historyList, nameFilter); 
    if(idx != -1) { 
     historyList.splice(idx, 1); 
    } 
}; 


// ---------------------- Tests ----------------------------------- 
addToHistory("history1", "Note of history1"); 
addToHistory("history2", "Note of history2"); 
addToHistory("history3", "Note of history3"); 
addToHistory("history4", "Note of history4"); 

alert(historyList); // history1,history2,history3,history4 

removeHistory("Name", "history1"); 

alert(historyList); // history2,history3,history4 

removeHistory("Notes", "Note of history3"); 

alert(historyList); // history2,history4 
0

peut être fonction inline Le Array.prototype.remove entier comme suit:

function removeEntry(value) { 
    historyList = $.grep(historyList, function(item) { 
    return (item.ID !== value); 
    }); 
} 

Vous pouvez, bien sûr, créer une fonction pour envelopper $.grep ou le prédicat que vous le souhaitez. (Et si vous le faites, vous ne voulez probablement pas modifier Array.prototype, préférer la modification historyList lui-même (en utilisant Array.prototype.splice) ou la création d'une (fonction probablement statique) ailleurs.)

En outre, ce problème ne concerne pas la question SO vous mentionnez dans votre question, autant que je sache.

Questions connexes