0

Using nodejs in Ubuntu. I've been reading the MDN docs for the JavaScript forEach() method. I understand there are other ways to do this, but I learn by doing; I'm trying to make the array copy a unique collection of the values in the arr array; no duplicates. I want to do this using the forEach() method.

The setup:

var arr = [1, 2, 3, 4, 4, 3, 2];
var copy = [];

So why does this work?

copy.includes(1); // returns false

While this doesn't?

arr.forEach(function(element, copy) {
 if (!copy.includes(element)) {
   copy.push(element);
 }
});

And here is the error:

TypeError: copy.includes is not a function
    at repl:2:11
    at Array.forEach (native)
    at repl:1:5
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:339:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:536:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:191:7)
MadHatter
  • 366
  • 1
  • 7
  • 23
  • 1
    Learning by doing and using Stackoverflow is a great way to learn JavaScript. You might also enjoy the alternative methods to remove duplicates described in this post: https://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – John Nov 07 '17 at 21:29

2 Answers2

3

Try:

arr.forEach(function(element) {
 if (!copied.includes(element)) {
   copied.push(element);
 }
});

Second argument of forEach callback is index, not array you're trying to fill. Also, you referenced copy which is undefined, while correct variable named is copied according to your code example.

Edit

After you've edited your code you use name copy both for array and second argument of forEach callback (why the heck you need second argument of forEach callback - which by the way - is index, not "copy" - whatever copy you mean : P).

So, you get an error that Number.prototype doesn't have method called includes() which is true, because index is a Number.

To sum up:

arr.forEach(function(element) {
 if (!copy.includes(element)) {
   copy.push(element);
 }
});
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • Please see the edit. The array of unique values is always called `copy`. That was an editing error on my part; my apologies. – MadHatter Nov 07 '17 at 21:00
  • 1
    You name 2 variables exactly the same. You can't call second argument of forEach callback also a `copy`. – Daniel Kmak Nov 07 '17 at 21:01
  • 2
    @SeanValdivia You haven't fixed the problem. You have `copy` here for no apparent reason: `function(element, copy) <----` – JLRishe Nov 07 '17 at 21:03
1

The second argument of forEach callback is the index, And by mentioning copy as a second argument, you are getting the index and not the array declare before. So what you are trying to do is 0.includes which actually is not a function. Removing the second argument will solve your problem

arr.forEach(function(element) {
 if (!copy.includes(element)) {
copy.push(element);
}
});
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31
  • 1
    That did the trick; I now see how I misunderstood the docs. Why are the MDN docs so unfriendly to noobs? I spent two hours reading up about the `forEach()` method. – MadHatter Nov 07 '17 at 21:06