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)