0
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)) ; //result is [4]

Goal : to take any array as first argument and remove whatever you want from said array as next argument.

Sorry this is keeping me from sleeping but what is [i] doing attached toRemove?

Lets say I want to do removeFromArray(['a','b','c'], 'b');

This will mean toRemove is equal to 'b'. The length of toRemove will be 1. So the loop says run for one iteration because toRemove is only 1 length.

To this point I think I understand. I don't get why (modifiedArray.includes(toRemove[i])) because doesn't that just mean (modifiedArray.includes('b'[1])) ?

And if you do a like removeFromArray(['a','b','c'], 'a', 'c'); Would the [i] mean that toRemove will get iterated twice, once for each, so for the second iteration its value would be 2?

(modifiedArray.includes(['a', 'c'][1]))
(modifiedArray.includes(['a' ,'c'][2]))

or would it be

(modifiedArray.includes(['a' ,'c'][1]))
(modifiedArray.includes(['a' ,'c'][1]))

I hope I was able to kind of explain my confusion I'm a bit frustrated. Thanks!

James Ross
  • 47
  • 5
  • 1
    _"This will mean toRemove is equal to 'b'."_ - No, it would be `[ 'b' ]` (or `[ 'a', 'c' ]` for your second example), because that's what the `...` does -> [Rest parameters - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) – Andreas Jul 15 '20 at 10:50
  • 2
    Does this answer your question? [What is the meaning of "...args" (three dots) in a function definition?](https://stackoverflow.com/questions/42184674/what-is-the-meaning-of-args-three-dots-in-a-function-definition) – VLAZ Jul 15 '20 at 10:51
  • Okay that makes more sense but still, the [i]? – James Ross Jul 15 '20 at 10:52
  • How else would you access the elements of an array in a `for`loop? – Andreas Jul 15 '20 at 10:53
  • I'm sorry I'm the one asking the questions here, mate. <3. So with my newfound knowledge I understand that we made toRemove an array let's stick with [ 'a', 'c' ]. Still a simpler concept to grasp. The purpose of the [i] is my actual question. Why its there what its doing yata yata, what changes without it.... – James Ross Jul 15 '20 at 11:01
  • I think you mistake @Andreas' question for an actual question, rather than something to help you learn. – Michelle Jul 15 '20 at 11:05
  • No I don't mistake it. I get I can fish around for 'accessing elements of an array' but if he knows the answer why not just say it. I'm burnt out and thought that response would get me the quickest answer. – James Ross Jul 15 '20 at 11:10

3 Answers3

0

That's simply because of the rest parameter

From MDN Documentation

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

so in your example your arguments passed to your removeFromArrayfunction (3,5) become an array named toRemove and in order to access the values you would have to do remove[i].

here is an example to help you clarify

const removeFromArray = function(a, b) {
  console.log(a)
  console.log(b)
}
removeFromArray(1, 3)

adding the spread operator and passing the values as a single argument

 const removeFromArray = function(...toRemove){
  console.log(toRemove)
}
  removeFromArray(1,3)
Sven.hig
  • 4,315
  • 2
  • 5
  • 18
  • I much better understand rest parameters now, but why can't their values be accessed in a for loop without the [i] attached? – James Ross Jul 15 '20 at 11:11
  • because that's the only way to access values from an array, how would you access the values in array? – Sven.hig Jul 15 '20 at 11:18
  • You can also use `for ..of` loop to iterate over an array. – Darvesh Jul 15 '20 at 11:18
  • @slooo7 in OP's example there is already a for loop – Sven.hig Jul 15 '20 at 11:21
  • I plan to read about for..of tomorrow thank you. This is not my code so I don't really understand why I need the [i] if in my original code the rest parameter makes it an array then why can't I reference that array without [i] and what value does [i] have when the function runs? I get its necessary or the only way but I don't get why... – James Ross Jul 15 '20 at 11:23
  • @James Ross because to access the `values` of an array in a `loop` you have to iterate through the `array `and proving an `index` to each element in this case the index is ` i` and inside the loop each time `i` value changes starting from 0 all the way to the length of the array-1 – Sven.hig Jul 15 '20 at 11:26
  • @JamesRoss here is a link to learn more about [arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) – Sven.hig Jul 15 '20 at 11:28
  • 1
    _"...and what value does [i] have when the function runs?"_ - If you don't know how a `for` loop works then you really should have a look at its documentation (e.g. [here](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for)) or grab a tutorial on how to work with arrays – Andreas Jul 15 '20 at 11:28
  • @JamesRoss I highly recommend you taking @Andreas's advise on board and find a good tutorial about ` arrays`, `objects` , `for of loop` – Sven.hig Jul 15 '20 at 11:32
  • thanks fellas I was being stupid but it all came back to me after solooo broke it down. I wasn't processing that toRemove was an array and [i] was accessing value via looping by length. I feel like a monkey who tied a knot but oh well. – James Ross Jul 15 '20 at 11:36
  • I will look into as much as I can its hard because I'm trying to teach myself but keep ending up with real life pulling me away and then forgetting stuff like how to access values in arrays cause I haven't done it in a month. Its frustrating. Thanks again I'll read up. – James Ross Jul 15 '20 at 11:38
  • @James Ross Just keep practicing you will be fine – Sven.hig Jul 15 '20 at 11:39
0

const removeFromArray = function (firstArray, ...toRemove) {

Read about Rest parameters first.

firstArray will be first argument you are passing to this function.

...toRemove takes rest of the argument as an Array.

If you call the function this way removeFromArray([1,3,4], 4,5,6,7),

firstArray will be equal to [1, 3, 4]

toRemove will be equal to [4, 5, 6, 7]

let modifiedArray = [...firstArray]; // this will create a copy of the firstArray and store it in modifiedArray.

for (let i = 0; i < toRemove.length; i++) {

This will loop over the toRemove array whose value is [4, 5, 6, 7]

if (modifiedArray.includes(toRemove[i])) {

In the first iteration, it will check toRemove[0] which is 4 exists in modifiedArray, if it exists it will remove that element from the modifiedArray in the next line. In the second iteration it will check toRemove[1] which is 5 and remove it from modifiedArray likewise it will go on removing all the elements present in toRemove from modifiedArray.

modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1);

At the end it will remove all the elements which the array toRemove contains from the modifiedArray which is a copy of firstArray and return it.

Darvesh
  • 644
  • 6
  • 13
  • You're a saint thank you. I had gotten so locked into thinking about it one way but I see now what is happening and will sleep much happier. – James Ross Jul 15 '20 at 11:27
0

toRemove.lengthdetermines the number of cycles,Each value in toremove is executed once.

There's another important thing,indexOf only shows index of the first lookup object. So, if there are more than one same number in the array, it will not be deleted.

['a','b','b'].indexOf('b')

I'm not good at English. I can only talk to you so much.

Jinweizhu
  • 149
  • 5