-2

This is my first time asking a question so go easy on me. I am currently in class learning JavaScript and am loving it so far. I am having issues with one of our assignments however.

I have an array with 4 objects inside of it. My current goal is to remove a specific object from the array. It's not on the end so I would assume I need to use a for loop to do so, but am having trouble with the syntax. My current code is as follows:

var devMountainEmployees = [];

var tyler = {
    name: 'Tyler', 
    position: 'Lead Instructor/Engineer', 
    spiritAnimal: 'Honey Badger'    
};

var cahlan = {
    name: 'Cahlan', 
    position: 'CEO', 
    spiritAnimal: 'butterfly'    
};

var ryan = {
    name: 'Ryan', 
    position: 'Marketing', 
    spiritAnimal: 'fox'    
};

var colt = {
    name: 'Colt',
    position: 'Everything really',
    spiritAnimal: 'Young Male Horse'
}

devMountainEmployees.push(tyler, cahlan, ryan, colt);
console.log(devMountainEmployees.length);

The one I want to remove is cahlan and I need to use a loop.
Thanks for your help guys.

3
  • 1
    So where are you stuck? If this is your assignment, why are you asking others to do it for you? Commented Jul 5, 2014 at 2:55
  • I apologize if it appears I am asking to straight up do my assignment for me. That is not my intention. I can solve it perfectly well just using splice but I wanted to know if I could do it via a loop. I was looking at that other question you posted and I think it might help me Commented Jul 5, 2014 at 2:59
  • No big deal, just that if you have an assignment to do it as a loop, it's a good idea to show what you've tried and where you're stuck. Best of luck. Commented Jul 5, 2014 at 14:54

2 Answers 2

2

You don't need a loop to remove a known object from an array, you can find the offset with indexOf and use splice to remove it.

devMountainEmployees.splice(devMountainEmployees.indexOf(cahlan), 1);

UPDATE:

Since you need to use a loop, do this:

for(var i = 0; i < devMountainEmployees.length; i++)
{
    if(devMountainEmployees[i] === cahlan)
    {
        devMountainEmployees.splice(i, 1);
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Loop through your devMountainEmployees until you find cahlan, then remove him from the array. This was the instructions I was given. Thanks for the help though
@user3806863: Opinions on this requirement aside, I've added an alternative using a for loop to find the index.
1

If you know the index of the object you would like to remove, then you can use splice

devMountainEmployees.splice(1, 1) (first is the index, second is the number to remove)

4 Comments

While this works perfectly, I think the point of this is that I need to use a loop to do it.
That's ridiculous. Why would your computer class reinvent the wheel like that?
I think there point is to give us practice with loops. Which apparently I need since I was having issues with it.
@Strikeskids - I think the point is to write code that doesn't know the index in advance. So OP will still need .splice() but must first search for the cahlan element to get its index. (Which I would do with .indexOf() rather than a loop.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.