53

How do I remove one item based on both the courseID and endDate from the following javascript object?

    window.MyCheckedCourses = [
        { courseID: '123', endDate: '6/7/2010' },
        { courseID: '123', endDate: '3/9/2003' },
        { courseID: '456', endDate: '3/9/2003' }  
    ]; 
5
  • 6
    that's not an object, it's an "array" of objects. Commented Aug 16, 2013 at 14:09
  • 1
    window.MyCheckedCourses is an array, not a jQuery object. Commented Aug 16, 2013 at 14:09
  • 2
    As everyone else has said, that's an array of objects, and has nothing to do with jQuery. You case just use MyCheckedCourses.splice(startIndex, count); to remove whichever elements you want from the array. Commented Aug 16, 2013 at 14:11
  • 1
    Thanks so much for everyone's help. How would I remove a single object from the JavaScript array of objects based on both the courseID and endDate values of the object I want removed? Commented Aug 16, 2013 at 14:13
  • 1
    @AllanHorwitz Loop over the array. Access the item's properties as you've specified, and check whether they match the desired values. Then use .splice(). Be careful though, in case there's multiple matches, you'll need to loop from the length of the array to 0 Commented Aug 16, 2013 at 14:14

2 Answers 2

29

Iteration is a must. You have to use .splice() to remove corresponding item and break the for loop.

var i, id = '123', date = '6/7/2010';
for(var i = 0, il = MyCheckedCourses.length;i<il;i++) {
    if(MyCheckedCourses[i].courseID == id && MyCheckedCourses[i].endDate == date) {
        MyCheckedCourses.splice(i, 1);
        break;
    }
}

You can make a function and use it with parameters like this;

function remove(id, date) {
    for(var i = 0, il = MyCheckedCourses.length;i<il;i++) {
        if(MyCheckedCourses[i].courseID == id && MyCheckedCourses[i].endDate == date) {
            MyCheckedCourses.splice(i, 1);
            break;
        }
    }
}
// Example usage:
remove('123', '6/7/2010');

Edit after Ian's comment:

I assume that your collection have unique items. If not you have to iterate through all items and you have to do it backwards because if you remove an element from array it's index will change and iteration will not work correctly. So this function is a much more safer version;

function remove(id, date) {
    for(var i = MyCheckedCourses.length - 1;i >= 0;i--) {
        if(MyCheckedCourses[i].courseID == id && MyCheckedCourses[i].endDate == date) {
            MyCheckedCourses.splice(i, 1);
        }
    }
}
// Example usage:
remove('123', '6/7/2010');
Sign up to request clarification or add additional context in comments.

3 Comments

Although id makes me think of "unique" (and I know the title says "single object", but that could mean a "single object" in several places), if there are possible multiple matches, you'll have to (remove the break; and) loop backwards
You're right Ian, I'm going to update my answer.
Just use filter. Here is an example : MyCheckedCourses.filter(x=>{ return !(x.courseID == '123' && x.endDate == '6/7/2010')})
3

You can delete an element from an array using splice: MyCheckedCourses.splice(index,length);

An example:

MyCheckedCourses=[0,1,2,3];
MyCheckedCourses.splice(1,1);

MyCheckedCourses is now: [0, 1, 3]

To find the index based on key values you can use:

// only returns the first found index
function findBy(arr,keys){
  var i = 0,match,len;
  for(i=0,len=arr.length;i<len;i++){
     match=true;
     for(key in keys){
       if(arr[i][key]!==keys[key]){
         match=false;
         break
       }
     }
     if(match===true){
       return i;
     }
  }
  return false;
}
var courses=[
    { courseID: '123', endDate: '6/7/2010' },
    { courseID: '123', endDate: '3/9/2003' },
    { courseID: '456', endDate: '3/9/2003' }  
  ];
var index = findBy(courses,
  {courseID:"123",
   endDate:"3/9/2003"}
);
if(index!==false){
  courses.splice(index,1);
}
console.log(courses);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.