Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a Single Object from a Javascript Object [duplicate]

Tags:

javascript

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' }       ];  
like image 611
ADH Avatar asked Aug 16 '13 14:08

ADH


People also ask

How do you remove duplicate objects?

To remove the duplicates from an array of objects:Create an empty array that will store the unique object IDs. Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.

Does Set remove duplicates JavaScript?

A Set is a collection of unique values. To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements.

Can JavaScript set have duplicate values?

The Set object lets you store unique values of any type, whether primitive values or object references. you are passing new object not reference so it is allowing to add duplicate.


2 Answers

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'); 
like image 87
Emre Erkan Avatar answered Sep 28 '22 05:09

Emre Erkan


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); 
like image 31
HMR Avatar answered Sep 28 '22 05:09

HMR