Fundamentals 4 Odin removeFromArray syntax questions?
const removeFromArray = function(firstArray,...toRemove) { let modifiedArray = [...firstArray]; for (i = 0; i < firstArray.length; i++) { if (modifiedArray.includes(toRemove[i])) { modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1) } } return modifiedArray; }
This is a project from odin fundamentals 4 for reference. I accidentally found the solution in my impatience while trying to figure out how to do it. The goal of said function is to input an array of whatever you like and remove whatever you like from said array via a function with 2 parameters. Ie I input removeFromArray([3,4,5], 3) and will be returned [4,5]
Question 1) Why are the brackets necessary around […firstArray] Question 2) If [i] is being used to loop over how many iterations you decide to input for the first parameter (an array), why is it being attached to (toRemove[i])?
Maybe I have the wrong idea of how this is actually working as a whole? Help appreciated, thanks!
Tests:
const removeFromArray = require('./removeFromArray') describe('removeFromArray', function() { it('removes a single value', function() { expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]); }); it('removes multiple values', function() { expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]); }); it('ignores non present values', function() { expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]); }); it('ignores non present values, but still works', function() { expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]); }); it('can remove all values', function() { expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]); }); it('works with strings', function() { expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]); }); it('only removes same type', function() { expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); }); });
const removeFromArray = function(firstArray,...toRemove) { let modifiedArray = [...firstArray]; for (let i = 0; i < toRemove.length; i++) { if (modifiedArray.includes(toRemove[i])) { modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1) } } return modifiedArray; }; console.log(removeFromArray([3,4,5], 3,5))
Question 1) Why are the brackets necessary around […firstArray]
Because without them it’s a syntax error. That form of spread notation is only valid within an array literal. (Objects have the same notation, but it means something different.) ...
isn’t an operator, it’s syntax that’s only defined in certain places (array literals, object literals, destructuring patterns, function argument lists, and function parameter lists).
Question 2) If [i] is being used to loop over how many iterations you decide to input for the first parameter (an array), why is it being attached to (toRemove[i])?
It shouldn’t be, that’s incorrect. It happens to work if the array is longer than the number of things you’re trying to remove from it because you’re going from 0
to < firstArray.length
and so if that’s as many or more than the number of entries in toRemove
, you see all of toRemove
‘s entries (and undefined
s, if firstArray
is longer). Here’s an example of that code not working:
const removeFromArray = function(firstArray, ...toRemove) { let modifiedArray = [...firstArray]; for (i = 0; i < firstArray.length; i++) { if (modifiedArray.includes(toRemove[i])) { modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1) } } return modifiedArray; } console.log(removeFromArray([1, 2], 3, 4, 1)); // Should be [2], is actually [1, 2]
Instead, the for
loop should be through < toRemove.length
.
Another issue with that code is that it never declares i
, so it’s falling prey to what I call The Horror of Implicit Globals. I recommend using strict mode to make that the error it always should have been:
"use strict"; const removeFromArray = function(firstArray, ...toRemove) { let modifiedArray = [...firstArray]; for (i = 0; i < firstArray.length; i++) { if (modifiedArray.includes(toRemove[i])) { modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1) } } return modifiedArray; } console.log(removeFromArray([1, 2], 3, 4, 1)); // Should be [2], is actually [1, 2]