jquery javascript remove object data from JSON object

前端 未结 3 1991
迷失自我
迷失自我 2021-01-03 14:01

I have JSON Object that looks something like the below object, this object can go on for days. So I am wondering is there anyway I can delete full set a set being the equiva

相关标签:
3条回答
  • 2021-01-03 14:30

    It worker for me.

      arrList = $.grep(arrList, function (e) { 
    
            if(e.add_task == addTask && e.worker_id == worker_id) {
                return false;
            } else {
                return true;
            }
        });
    

    It returns an array without that object.

    Hope it helps.

    0 讨论(0)
  • 2021-01-03 14:40

    You could write a removeFirst function like this:

    function removeFirst(arr, func)
        {
            for (var i=0; i<arr.length; i++)
            {
                if (func.call(arr[i]))
                {
                    arr.splice(i,1);
                    return arr;
                }
            }
        }
    

    and then call it:

    removeFirst(locations, function(){return this.zipcode=="06238";});
    

    If you want to remove more then one element, you could write it like this:

    function removeMany(arr, func)
        {
            for (var i=arr.length; i>0; --i)
            {
                if (func.call(arr[i]))
                {
                    arr.splice(i,1);
                }
            }
            return arr;
        }
    

    and use it in the same way.

    Alternatively, use underscore (http://documentcloud.github.com/underscore) reject method in a similar way:

    _.reject(locations, function(location){ return location.zipcode=="06238";});
    

    Underscore is pretty good for doing array manipulations.

    0 讨论(0)
  • 2021-01-03 14:49

    Simply just pass the index

    delete locations[0];
    

    You go through a normal JSON iteration like this

    jQuery.each(locations, function(i, val) {
       if(val.zipcode == "yourvalue") // delete index
       {
          delete locations[i];
      }
    });
    

    something like that. are you looking for an idea or a complete function.

    Here is the jsFiddle

    0 讨论(0)
提交回复
热议问题