0

I'm trying to remove object from array, I've tried and don't seem to be getting just right. So would someone be able to look at my code used

Code:

Array inside files ->

[{
  "favouriteLinkContent": "Group",
  "favouriteLinkID": "groupBTNFav"
}, {
  "favouriteLinkContent": "Server",
  "favouriteLinkID": "serverBTNFav"
}, {
  "favouriteLinkContent": "User",
  "favouriteLinkID": "userBTNFav"
}, {
  "favouriteLinkContent": "Sync",
  "favouriteLinkID": "syncBTNFav"
}]

$(document).on('click', '.removeFavourite', function ()
               {

  var buttonValue = $(this).closest('a')[0].innerText;

  // Reading
  $.ajax(
    {
      global: false,
      type: "POST",
      cache: false,
      dataType: "json",
      data: (
        {
          action: 'read'
        }),
      url: 'php/saveFavouriteLinks.php',
      success: function (data)
      {

        $.each(data, function (key, value)
        {
          newValue = ' ' + value['favouriteLinkContent'];

          if (newValue == buttonValue)
          {
            console.log(value['favouriteLinkContent']);
            delete value['favouriteLinkContent']; 
          }
          //$("#favouritesList").append("<li><a id=" + value['favouriteLinkID'] + "><i class='fa fa-dashboard fa-fw'></i> " + value['favouriteLinkContent'] + "<i class='glyphicon glyphicon-minus pull-right removeFavourite'></i></a></li>");
        }); 
      }
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Example of what I want happening

Button Click -> Button value is Group -> Remove Group from Array ->

{"favouriteLinkContent":"Group","favouriteLinkID":"groupBTNFav"}

-> After deletion ->

[{
  "favouriteLinkContent": "Server",
  "favouriteLinkID": "serverBTNFav"
}, {
  "favouriteLinkContent": "User",
  "favouriteLinkID": "userBTNFav"
}, {
  "favouriteLinkContent": "Sync",
  "favouriteLinkID": "syncBTNFav"
}]
Dhiraj
  • 33,140
  • 10
  • 61
  • 78
YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • 1
    Can you reproduce this on jsfiddle? – Tushar Jun 01 '15 at 14:45
  • @Tushar No need, I've just fixed it myself. My stupid mistake, I ended up putting the class on the wrong element. My mistake :) – YaBCK Jun 01 '15 at 14:47
  • @Tushar i've changed my question on this post, could you check it out? – YaBCK Jun 01 '15 at 15:07
  • 2
    on what basis do you want to remove the element from the array? Check this http://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript ? – Dhiraj Jun 01 '15 at 15:09
  • for removing element you can use [_slice_](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) and [_splice_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) functions, and also you can use [_filter_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) function – Grundy Jun 01 '15 at 15:13
  • @DhirajBodicherla i've updated my question to provide you with abit more information what I want happening – YaBCK Jun 01 '15 at 15:14
  • @Chris, you always want remove only one element, or a few elements? – Grundy Jun 01 '15 at 15:16
  • @Grundy I want to remove the "favouriteLinkContent" and the "favouriteLinkID". Depending on which value gets passed – YaBCK Jun 01 '15 at 15:17
  • also, how you want use filtered array? possibly you not need remove, and just skip in loop – Grundy Jun 01 '15 at 15:17

3 Answers3

2

You could use a helper function with Array filter:

var removeObjectFromArray = function(array, property, objName){          
    return array.filter(function(e){
        return e[property] != objName;
    });
}


 console.log(removeObjectFromArray(array, 'favouriteLinkContent', 'Group'));

JSFiddle

Felix
  • 3,058
  • 6
  • 43
  • 53
1

You can use splice

for(var i=0;i<arr.length;i++){
  if(arr[i].favouriteLinkContent === "Group"){ // <---- if you want to remove Group
    array.splice(i, 1); 
    i--;  // <--- have to do i-- because splice changes the array and array size will be altered
  }
}

You can wrap it in a function and remove the element from array based on the item

removeElement(arr, "Group");

function removeElement(arr, item) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].favouriteLinkContent === item) { 
      array.splice(i, 1);
      i--;
    }
  }
}
Dhiraj
  • 33,140
  • 10
  • 61
  • 78
1

You can use filter function https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Array/filter

function removeGroup (arr, groupName) {
    return arr.filter(function (el) {return el.favouriteLinkContent !== groupName;});
}

Hope it helps, Dan

Daniel Rosano
  • 695
  • 5
  • 16