代码之家  ›  专栏  ›  技术社区  ›  Martin Ongtangco

如何删除json格式的javascript数组中的项

  •  0
  • Martin Ongtangco  · 技术社区  · 14 年前

    我有这个json结构,我把它做成了一个数组。我试图删除条目,但此代码对我无效: 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;
                });
            };
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   naikus    14 年前

    在FF中的快速测试表明它是有效的!

    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
    
        2
  •  0
  •   strager    14 年前

    整个 Array.prototype.remove

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

    当然,您可以创建一个要包装的函数 $.grep 或者你想要的谓词(如果你这样做了,你可能不想修改 Array.prototype ; 更喜欢修改 historyList Array.prototype.splice )或者在别处创建一个(可能是静态的)函数。)

    而且,据我所知,这个问题与你在问题中提到的那个问题无关。