나는
var array1 = ["something9","something10","something11"]
단순히 array1의 각 요소에서 후행 번호에서 array2를 만들고 싶습니다.
charAt ()을 사용하여 마지막 문자를 얻을 수 있었지만 각 요소 대신 array1에서 첫 번째 요소 만 가져올 수있었습니다.
이것이 자바 스크립트에서 가능할까요?
두 번째 배열을 만들려면 첫 번째 배열을 반복해야합니다.
Array.map () 을 사용 하여 간단한 반복으로 수행 할 수 있습니다.
var array1 = ["something9", "something10", "something11", 'asdf'];
var array2 = array1.map(function(value) {
var match = value.match(/\d+$/);
return match ? match[0] : ''
});
snippet.log(JSON.stringify(array2));
console.log(array2)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다