Remove object from array

Hello, I have an array of objects called Links where each object contains: name, url. How can I remove an object from the array when passing in both the name and url?

For example the array contains:

Microsoft, www.microsoft.com
experts exchange, www.experts-exchange.com

If I pass in Microsoft, www.microsoft.com the array would only contain experts exchange, www.experts-exchange.com....
Scott BaldridgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
you can loop through your array and check the value of the object and use splice

here is an Example in action

http://jsfiddle.net/umairsaleemid/mkjqM/
0
Scott BaldridgeAuthor Commented:
Hello, I'm trying to do something like this :

  for (var i = 0 ; i < _LinksStorage.length; i++) {
                     if (_LinksStorage[i].name === Name && _LinksStorage[i].url === Url)
                            _LinksStorage.splice(i);
                }

Open in new window

but getting this error
Uncaught TypeError: Cannot read property 'name' of undefined

It is looping through the collection twice but then the exception. There are only two objects in the collection.

Any ideas?
0
plusone3055Commented:
If there are only those two names in the array just use and if else statement

IE

If  .name = microsoft then
splice (0)
else if  .name = experts-exchange then
splice (1)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Scott BaldridgeAuthor Commented:
I won't know what is in the array as it is being populate from the database via json. This is the complete code for my event handler. On click of the delete link I'm removing the object from the dom and also need to remove it from the array which is populated when the page loads from the database.

 $(document).on('click', '.deleteLink', function (e) {
                
                var tr = $(this).closest('tr');
                var Name = tr.find('.link').text();
                var Url = tr.find('.link').attr('href');
               
                for (var i = 0 ; i < _LinksStorage.length; i++) {
                     if (_LinksStorage[i].name === Name && _LinksStorage[i].url === Url)
                            _LinksStorage.splice(i);
                }

                var removeItem = $(this);
                removeItem.closest("tr").remove();
              
             });

Open in new window

0
plusone3055Commented:
have you attempted without the Name element in your if statement

$(document).on('click', '.deleteLink', function (e) {
                
                var tr = $(this).closest('tr');
                var Name = tr.find('.link').text();
                var Url = tr.find('.link').attr('href');
               
                for (var i = 0 ; i < _LinksStorage.length; i++) {
                     if (_LinksStorage[i].url === Url)
                            _LinksStorage.splice(i);
                }

                var removeItem = $(this);
                removeItem.closest("tr").remove();
              
             });

Open in new window

0
Scott BaldridgeAuthor Commented:
Yes, I get the same result.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.