1

I want to remove a complete row from a 2D array in javascript where there is a specific id. I have searched and I cant find any examples. How can I achieve this?

I have an object / array like this...

[
{"id":"1","title":"Sample Event 1\n","start":"Nov 27 2011 10:30:00 GMT+0000 (GMT)","end":"Nov 27 2011 11:30:00 GMT+0000 (GMT)","allDay":false},
{"id":"2","title":"Sample Event 2","start":"Nov 28 2011 12:30:00 GMT+0000 (GMT)","end":"Dec 01 2011 12:30:00 GMT+0000 (GMT)","allDay":true},
{"id":"3","title":"Sample Event 3\n","start":"Nov 27 2011 11:30:00 GMT+0000 (GMT)","end":"Nov 27 2011 12:30:00 GMT+0000 (GMT)","allDay":false}
]

My data is pulled from a mysql database via php and json_encoded like below...

var eventObject = <?php echo json_encode($events);?>;

My problem is, if i wanted to remove a complete row eg one where id == 1 for example how could i achieve this?

I have tried using the remove attribute but this failed.

I have also tried using splice in the following manner

var eventLocation = eventObject.indexOf(EventId);
eventObject.splice(11,1);

but to no avail. Please can anyone assist?

0

4 Answers 4

2
yourarray.filter(function(obj) { return obj.id != "1"; })

will work in modern browsers

Sign up to request clarification or add additional context in comments.

3 Comments

+1, people should start using these more. For < IE9 there is a good shim on the MDC page.
thanks i tried this in the following format however to no avail... one day i will grasp javascript. Do you mind taking a look? function deleteEvent(EventId){ var confirmDeleteEvent = confirm("Are you sure you want to delete this event?"); if(confirmDeleteEvent != true) { return false; } else { eventObject = eventObject.filter(function() { return el.id != EventId}); alert("deleted"+EventId); } }
What exactly does not work, are you getting error messages? It seems fine to me. However, you might shorten your code to if( !confirm("Are you sure?") ) return false; /*else*/ search'n'splice().
2

You could loop through them, checking for the ID, and if you find it use splice.

for(var i = 0; i < eventObject.length; i++) {
    if (eventObject[i].id == "1") {
        eventObject.splice(i, 1);
        break;
    }
}

console.log(eventObject);

http://jsfiddle.net/SCfaP/

2 Comments

thanks i tried using this in the following format however to no avail im really getting stuck here :( any idea function deleteEvent(EventId){ var confirmDeleteEvent = confirm("Are you sure you want to delete this event?"); if(confirmDeleteEvent != true) { return false; } else { for(var i = 0; i < eventObject.length; i++) { if (eventObject[i].id == EventId) { eventObject.splice(i, 1); break; } } console.log(eventObject); alert("deleted"+EventId); } }
@JSweete please reproduce the problem on jsfiddle.net and send me the link.
0

You are contolling the $events object. Why don't you structure it like

eventObject = {
    "1": {"title":"Sample Event 1\n","start":"Nov 27 2011 10:30:00 GMT+0000 (GMT)","end":"Nov 27 2011 11:30:00 GMT+0000 (GMT)","allDay":false},
    "2": {"title":"Sample Event 2","start":"Nov 28 2011 12:30:00 GMT+0000 (GMT)","end":"Dec 01 2011 12:30:00 GMT+0000 (GMT)","allDay":true},
    "3": {"title":"Sample Event 3\n","start":"Nov 27 2011 11:30:00 GMT+0000 (GMT)","end":"Nov 27 2011 12:30:00 GMT+0000 (GMT)","allDay":false}
};

instead of eventArray? With that you could easily use delete eventObject[toRemove]. Or are you going to get problems with the sort order then?

1 Comment

unfortunately the calendar i am using requires the object to be structured in the above format. Ive some how managed to play with the above given coding samples and ive some how got it working now, so thank you very much for your help and thankyou everyone that contributed :) p.s. i hate javascript.
0

@Alex Turpin modifying your answer to traverse the array in reverse when there are more than 1 matches for "1".

var loops = eventObject.length-1;
for(var i = loops; i > -1; i--) {
    if (eventObject[i].id == "1") {
        eventObject.splice(i, 1);
    }
}
console.log(eventObject);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.