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"
}]
9
  • 1
    Can you reproduce this on jsfiddle? Commented Jun 1, 2015 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 :) Commented Jun 1, 2015 at 14:47
  • @Tushar i've changed my question on this post, could you check it out? Commented Jun 1, 2015 at 15:07
  • 2
    on what basis do you want to remove the element from the array? Check this stackoverflow.com/questions/10024866/… ? Commented Jun 1, 2015 at 15:09
  • for removing element you can use slice and splice functions, and also you can use filter function Commented Jun 1, 2015 at 15:13

3 Answers 3

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

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

Comments

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--;
    }
  }
}

3 Comments

methinks you a bit confused with params and should be array.splice(i,1);, because first param: start index, and second - deleting count
me thinks you are right and thanks for correcting :)
Cheers @DhirajBodicherla for the answer
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

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.