-1

I have an array and here's how it looks:

{id:"1", videotype:"youtube", videoId:"y685gVGRQ98"},{id:"2", videotype:"youtube", videoId:"CtjuDJytD18"} 

Each entry has an Id.

I need a function to remove all entries belonging to the chosen Id.

for example:

removeFromArray(2);

It would then remove all this: id:"2", videotype:"youtube", videoId:"CtjuDJytD18"
Satch3000
  • 40,202
  • 83
  • 203
  • 337

3 Answers3

1

Modified for the updated question:

blacklistedId = 1;

newArray = oldArray.filter(function(value) {
    return value.id != blacklistedId;
});
overburn
  • 1,106
  • 8
  • 24
1

My question is...How do I do to remove a specific entry?

You can write another method

removeDataToArray: function(id, videotype, videoId) {
       var videoArray = JSON.parse(document.getElementById("videoLinksArray").innerHTML);
       //filter out item that matches the id, videoType and videoId
       videoArray = videoArray.filter( function(val){
          return !( val.id == id && val.videoType == videoType && val.videoId == videoId );
       });
       document.getElementById("videoLinksArray").innerHTML = JSON.stringify( videoArray );
},

It would be more like: removeDataFromArray(id) and it would know the unique id of the entry

Then the filter will change to

       videoArray = videoArray.filter( function(val){
          return val.id != id;
       });

Edit

removeFromArray(2);

It would then remove all this: id:"2", videotype:"youtube", videoId:"CtjuDJytD18"

removeDataToArray: function(id) {
       var videoArray = JSON.parse(document.getElementById("videoLinksArray").innerHTML);
       //filter out item that match the id
       videoArray = videoArray.filter( function(val){
          return val.id != String( id ) ;
       });
       document.getElementById("videoLinksArray").innerHTML = JSON.stringify( videoArray );
},
gurvinder372
  • 61,170
  • 7
  • 61
  • 75
0

Consider using utility library like underscore:

var desired_list = removeFromList([{id:"1", videotype:"youtube", videoId:"y685gVGRQ98"},{id:"2", videotype:"youtube", videoId:"CtjuDJytD18"}], "1");

function removeFromList(list, id) {
   return _.reject(list, function(val){return val.id == id});
}

enjoy

Mosd
  • 1,420
  • 18
  • 21