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)
1
  • 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: stackoverflow.com/questions/9229645/… Commented Nov 7, 2017 at 21:29

2 Answers 2

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);
 }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Please see the edit. The array of unique values is always called copy. That was an editing error on my part; my apologies.
You name 2 variables exactly the same. You can't call second argument of forEach callback also a copy.
@SeanValdivia You haven't fixed the problem. You have copy here for no apparent reason: function(element, copy) <----
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);
}
});

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.