요약
var arr1=[73,32,103,42,4];
var arr2=new Array(273,32,103,42,4);
arr1.push(2);
//앞쪽부터검색 없으면 -1리턴
var output1 = array.indexOf(4);
//출력
array.forEach(function(element,index,arr){
console.log(index+"/"+element);
});
for(var i in array){
console.log(array[i]);
}
==========================================================================
출처
<script type="text/javascript">
var arr1=[73,32,103,42,4];
var arr2=new Array(273,32,103,42,4);
console.log(arr1[0]);
console.log(arr2[0]);
console.log(arr1.length);
console.log(arr2.length);
var arr=[0,1];
arr.push(2);
arr.push(3);
arr.push(4);
alert(arr);
</script>
배열에 검색기능을 사용할수있다.
<script type="text/javascript">
var array=[1,2,3,4,5,5,4,3,2,1];
//앞쪽부터검색 없으면 -1리턴
var output1 = array.indexOf(4);
var output2 = array.indexOf(8);
//뒤쪽부터 검새 없으면 -1리턴
var output3 = array.lastIndexOf(4);
var output4 = array.lastIndexOf(8);
var output ="";
output+=output1+" : "+output2+"\n";
output+=output3+" : "+output4;
console.log(output);
</script>
배열의 또다른 기능
<script type="text/javascript">
var array=["가","나","다","라"];
array.forEach(function(element,index,arr){
console.log(index+"/"+element);
});
</script>
향상된 for문 배열
<script type="text/javascript">
var array=['포도','딸기','바나나','참외'];
for(var i in array){
console.log(array[i]);
}
</script>
'12 Javascript > 20 배열및 객체등' 카테고리의 다른 글
---21 배열 명령어 concat등 알아보기 (0) | 2021.07.29 |
---|---|
---20 자바스크립트 객체, 속성, 메소드 이해하기 (0) | 2021.07.29 |
객체의 활용 (객체내에 생성자, 변수(메소드)추가 삭제(delete)) (0) | 2021.07.15 |
10 객체 생성 및 접근 (for , in , with사용) (0) | 2021.07.15 |