Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Removing an items from this object

    I have on object what will be based around this

    Code:
    var JSON1 = {
                    "work": [
                        { "ID": 1, "reminderTitle":"1 w reminder" , "details":"1Some details blah blah blah", 
    "ID": 2, "reminderTitle":"2 w reminder" , "details":"2Some details blah blah blah" }
                    ],
                    
                    "home": [
                        { "ID": 1, "reminderTitle":"Third  h reminder" , "details":"3Some details blah blah blah" }
                    ]
               };
    I need to remove certain parts of it. I've seen people mention the splice command but I cant seem to get it to work.

    This is the code I have to display the list

    Code:
    $.each(JSON1[bName], function(i, v)
        {
            
            $('#dets').append('<p><b>' + v.reminderTitle + ': </b>' + v.details + ' <span class="fRight delete" id="' + v.ID + '" >Delete</span></p>');
        });
    I want it so when the user presses delete the whole row gets removed from the object. I'm basically looking from the javascript version of Mysql "DELETE FROM JSON1.work WHERE ID = x"

    How can I do this?
    Last edited by tomharto; 04-10-2013 at 09:52 AM.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by tomharto View Post
    I'm basically looking from the javascript version of Mysql "DELETE FROM JSON1.work WHERE ID = x"

    How can I do this?
    Code:
    var JSON1 = {
                    "work": [
                        { "ID": 1, "reminderTitle":"1 w reminder" , "details":"1Some details blah blah blah"}, 
                        { "ID": 2, "reminderTitle":"2 w reminder" , "details":"2Some details blah blah blah" }
                    ],
                    
                    "home": [
                        { "ID": 1, "reminderTitle":"Third  h reminder" , "details":"3Some details blah blah blah" }
                    ]
               };
    
    //l "DELETE FROM JSON1.work WHERE ID = x" 
    
    function delRow(obj, prop, val){
     var index=0;
      obj.some(function(a,b){if(a[prop]===val){ index=b; return 1;};})
      obj.splice(index,1);
     return obj;
    }
    
    
    // usage/test:
    delRow(JSON1.work, "ID", 1);
    console.log(JSON1)/*=={
    	"work": [
    		{
    			"ID": 2,
    			"reminderTitle": "2 w reminder",
    			"details": "2Some details blah blah blah"
    		}
    	],
    	"home": [
    		{
    			"ID": 1,
    			"reminderTitle": "Third  h reminder",
    			"details": "3Some details blah blah blah"
    		}
    	]
    } */
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •