-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.

TaylorAllred
  • 1,191
  • 2
  • 9
  • 20
  • 1
    So where are you stuck? If this is your assignment, why are you asking others to do it for you? – cookie monster Jul 05 '14 at 02: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 – TaylorAllred Jul 05 '14 at 02: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. – cookie monster Jul 05 '14 at 14:54

2 Answers2

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;
    }
}
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • 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 – TaylorAllred Jul 05 '14 at 02:54
  • @user3806863: Opinions on this requirement aside, I've added an alternative using a for loop to find the index. – Alexander O'Mara Jul 05 '14 at 02:56
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)

Strikeskids
  • 3,932
  • 13
  • 27
  • While this works perfectly, I think the point of this is that I need to use a loop to do it. – TaylorAllred Jul 05 '14 at 02:52
  • 1
    That's ridiculous. Why would your computer class reinvent the wheel like that? – Strikeskids Jul 05 '14 at 02:53
  • I think there point is to give us practice with loops. Which apparently I need since I was having issues with it. – TaylorAllred Jul 05 '14 at 03:00
  • 1
    @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.) – nnnnnn Jul 05 '14 at 03:04