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!

asked Jan 23, 2014 at 17:42
8
  • Before you return from the callback, save the object in a variable. It's not super clean but works. Commented Jan 23, 2014 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 Commented Jan 23, 2014 at 17:48
  • 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 ;) Commented Jan 23, 2014 at 17:54
  • 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. Commented Jan 21, 2016 at 4:52
  • 1
    @ThilakRaj plnkr.co/edit/ubAPmJoMQaKBLvWSsmTv?p=preview btw never name Object keys with capital letters ie Name always just name. Check out this Stackoverflow btw stackoverflow.com/questions/9882284/… Commented Jan 21, 2016 at 5:13

1 Answer 1

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);
answered Jan 23, 2014 at 17:57
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.