우분투에서 nodejs 사용. JavaScript forEach()
메서드에 대한 MDN 문서를 읽었습니다. 나는 이것을 할 수있는 다른 방법이 있다는 것을 이해하지만, 나는하는 것으로 배웁니다. 배열 copy
배열의 값을 arr
배열의 고유 한 컬렉션을 만들려고 노력하고있어; 중복이 없습니다. 나는 forEach()
방법을 사용하여 이것을하고 싶다.JavaScript forEach() 메소드 혼동
셋업 :
var arr = [1, 2, 3, 4, 4, 3, 2];
var copy = [];
왜이 일을 하는가?
copy.includes(1); // returns false
그래도 되나요? 여기
arr.forEach(function(element, copy) {
if (!copy.includes(element)) {
copy.push(element);
}
});
그리고
오류입니다 :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)
JavaScript를 배우는 가장 좋은 방법은 다음과 같습니다. 또한이 게시물에 설명 된 복제본을 제거하는 다른 방법을 이용할 수도 있습니다. https://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – John