JavaScript의 Array.prototype.map() 함수를 사용하여 호출된 함수의 결과로 새 배열을 만듭니다.
구문은 다음과 같습니다 -
arr.map(function callback(currentValue[, index[, array]])
이제 JavaScript에서 Array.prototype.map() 메서드를 구현해 보겠습니다.
예시
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click to display the abs() result...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
function display() {
var arr = [1, -2, 3, 4, 5, -6, 7, 8, 9, 10];
var res = arr.map(Math.abs);
document.write(res);
}
</script>
</body>
</html> 출력

위의 "결과" 버튼 클릭 -

예시
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click to round the numbers...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
var arr = [11.4, 12.9, 25.6, 88.9];
document.getElementById("test").innerHTML = arr
function display() {
var res = arr.map(Math.round);
document.getElementById("test").innerHTML = res
}
</script>
</body>
</html> 출력

숫자를 반올림하려면 "결과" 버튼을 클릭하십시오 -
