0

i have this json structure and I made it into an array. I was trying to remove the entry, but this code failed on me: Remove item from array if it exists in a 'disallowed words' array

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;
            });
        };
Community
  • 1
  • 1
Martin Ongtangco
  • 22,657
  • 16
  • 58
  • 84
  • Note: you're using an object literal, not JSON. JSON is something different. See: http://www.json.org/ – strager Aug 10 '10 at 06:37

2 Answers2

0

You could use a property filter to match the item in your history list. Below is the sample quick code to do so, I've modified your history list a bit.

A quick test in FF, shows that it works!

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
naikus
  • 24,302
  • 4
  • 42
  • 43
0

The entire Array.prototype.remove function can be inlined as follows:

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

You can, of course, create a function to wrap $.grep or the predicate as you wish. (And if you do, you probably don't want to modify Array.prototype; prefer modifying historyList itself (using Array.prototype.splice) or creating a (likely static) function elsewhere.)

Also, this problem does not relate to the SO question you mention in your question, as far as I can tell.

strager
  • 88,763
  • 26
  • 134
  • 176