0

Originally my question was How to Remove Object from jQuery array. < Found the answer, the one from @nnnnnn. Using $.grep

My new question now is how would you save the object that you just removed?

Reason is I wish to be able to push that saved Object back into the Array if the user decides to keep it.

My networks array:

console:
networks array = [object Object],[object Object]

networks: Array[2]
    0: Object
      count: 1
      id: "6"
      label: "CompanyName"
      type: "Organization"

    1: Object
      count: 1
      id: "12622"
      label: "MyGroup"
      type: "Group"

From the other answer I'm using this to look in the networks array, find the Object with the type of "Organization" and remove it.

networks = $.grep(networks, function(o,i) { return o.type === "Organization"; }, true);

Is there a simple way to save that entire Object? So it can be pushed back in?

Thanks for taking a look!

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • Before you return from the callback, save the object in a variable. It's not super clean but works. – Felix Kling Jan 23 '14 at 17:45
  • thanks for the tip, I just ran this function actually and the `$.grep` did not remove the object form the function :( may be back at square 1 – Leon Gaban Jan 23 '14 at 17:48
  • Oh wait lol, it does work `type not name`.. ok I'll try what you recommended – Leon Gaban Jan 23 '14 at 17:54
  • 3
    It should though, that's what `$.grep` is doing. Note that you are using `o.name` in the callback, but your example data doesn't have a `name` property. *edit:* Yep ;) – Felix Kling Jan 23 '14 at 17:54
  • another thing i want, There will be separate buttons for every object in array. if i want to delete that particular object in the array button clicked and that should be move to another secondary array. how to do it . i have used angular js ng-repeat to generate items. can you help me – Thilak Raj Jan 21 '16 at 04:49
  • 1
    @ThilakRaj that should be pretty easy, just check the object you clicked on. Compare some key in it, like `id` or `name` whatever, and you can use a simple `for loop` or `lodash` to find it in the main array and then remove it there. Use `splice`. – Leon Gaban Jan 21 '16 at 04:52
  • Can you show me using this plunker https://plnkr.co/edit/eKY6vrPtvaKstgcY0g3I?p=preview Secondary items will be displayed at right. primary array items will be displayed left – Thilak Raj Jan 21 '16 at 04:58
  • 1
    @ThilakRaj https://plnkr.co/edit/ubAPmJoMQaKBLvWSsmTv?p=preview btw never name Object keys with capital letters ie `Name` always just `name`. Check out this Stackoverflow btw http://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop – Leon Gaban Jan 21 '16 at 05:13

1 Answers1

3

You could assign the object inside the callback to a variable. This isn't a very clean solution, since such callbacks shouldn't have side effects IMO, but it works without making bigger changes to your code:

var obj;
networks = $.grep(networks, function(o,i) {
  if (o.type === "Organization") {
      obj = o;
      return true;
  }
  return false;
}, true);

or a bit shorter (and more confusing for people who don't know about the comma operator):

var obj;
networks = $.grep(networks, function(o,i) { 
    return o.type === "Organization" ? ((obj = o), true) : false;
}, true);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • another thing i want, There will be separate buttons for every object in array. if i want to delete that particular object in the array button clicked. how to do it . i have used angular js ng-repeat to generate items. can you help me – Thilak Raj Jan 21 '16 at 04:49