[JS] 07. Array

2017. 8. 28. 13:37·JS/JavaScript
728x90
반응형

07_Array.html

<script>
    //A simple array with constructor.
    var myArray1 = new Array("hello","world");
    //Literal declaration, the preferred way.
    var myArray2 = ["hello","world"];
    
    //Creating empty arrays and adding values
    var myArray = []; //값이 없는 배열 상태
    // Adds "hello" on index 0
    myArray.push("hello"); //배열 안에 값을 넣을 때
    // Adds "world" on index 1
    myArray.push("world");
    // Adds "!" on index 2
    myArray[2]= "!";
    
    //Leaving indices
    var myArray = [];
    myArray[0] = "hello"; //할당 연산자
    myArray[1] = "world";
    myArray[3] = "!";
    console.log(myArray); // ["hello", "world", undefined, "!"];
    
    //Accessing array items by index
    var myArray = ["hello", "world", "!"];
    console.log(myArray[2]); //"!"
    
    //[Array Methods and Properties]
    //Length of an array
    var myArray = ["hello", "world", "!"];
    console.log(myArray.length); //3, 값의 갯수 리턴
    
    var myArray = ["hello", "world", "!"];
    for(var i = 0; i<myArray.length; i++){
        console.log(myArray[i]); //각 인덱스에 있는 값들 활용
    }
    var myArray = [2,3,4];
    var myOtherArray = [4,5,6,7];
    var wholeArray = myArray.concat(myOtherArray); //concat 주어 객체, 목적어 객체를 통합하여 새로운 객체 생성  
    console.log(wholeArray);//[2,3,4,5,6,7]
    console.log(wholeArray.length); //7
    
    //Joining elements
    var myArray = ["hello", "world", "!"];
    //The default separator is a comma.
    console.log(myArray.join()); //"hello, world,!"
    console.log(myArray.join().length); //13
    //Any string can be used as separator...
    console.log(myArray.join(" ")); //"hello world !"; 띄어쓰기가 구분자 역할
    console.log(myArray.join("!!")); //"helloworld!!!"; 느낌표가 구분자 역할
    //...including an empty one.
    var arr = myArray.join("");
    console.log(arr);//"helloworld"
    console.log(arr.length); //1
    
    //Pushing and popping
    var myArray =[];
    myArray.push(0); //[0]
    myArray.push(2); //[0,2]
    myArray.push(7); //[0,2,7]
    myArray.pop(); //[0,2] 가장 위부터 삭제
    console.log(myArray);
    
    var myArray = ["world", "hello"];
    myArray.reverse(); //["hello", "world"]
    console.log(myArray);
    
    //Queue with shift() and push()
    var myArray = [];
    myArray.push(0); //[0]
    myArray.push(2); //[0, 2]
    myArray.push(7); //[0,2,7]
    myArray.shift(); //[2,7]
    console.log(myArray)
    
    //Slicing
    var myArray = [1,2,3,4,5,6,7,8];
    var newArray = myArray.slice(3);
    console.log(myArray); //[1,2,3,4,5,6,7,8]
    console.log(newArray); //[4,5,6,7,8]
    
    //myArray.splice(index, lengthj.values, ...);
    var myArray = [0,7,8,5];
    myArray.splice(1,2,1,2,3,4);
    console.log(myArray); //[0,1,2,3,4,5]
    
    //Sorting without comparing function.
    var myArray = [3,2,1];
    myArray.sort(); //1,2,3
    console.log(myArray);
    
    function descending(a,b){
        return b-a;
    }
    var myArray=[1,2,3];
    myArray.sort(descending); //[3,2,1]
    console.log(myArray);
    
    var myArray=[];
    myArray.unshift(0); //[0]
    myArray.unshift(2); //[2,0]
    myArray.unshift(7); //[7,2,0]
    console.log(myArray);
    
    //Native .forEach()
    function printElement(elem){
        console.log(elem);
    }
    function printElementAndIndex(elem, index){
        console.log("Index"+index+":"+elem);
    }
    function negateElement(elem, index, array){
        array[index] = -elem;
    }
    myArray=[1,2,3,4,5];
    //Prints all elements to the console
    myArray.forEach(printElement);
    //Prints "Index 0: 1", Index 1: 2", "Index 2: 3", ...
    myArray.forEach(printElementAndIndex);
    //myArray is now [-1,-2,-3,-4,-5]
    myArray.forEach(negateElement);
    console.log(myArray);
</script>
 

728x90
반응형

'JS > JavaScript' 카테고리의 다른 글

[JS] 09. Function  (0) 2017.08.28
[JS] 08. Object  (0) 2017.08.28
[JS] 06. Loop  (0) 2017.08.28
[JS] 05. Conditional_Code  (0) 2017.08.28
[JS] 04. Operator  (0) 2017.08.28
'JS/JavaScript' 카테고리의 다른 글
  • [JS] 09. Function
  • [JS] 08. Object
  • [JS] 06. Loop
  • [JS] 05. Conditional_Code
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (463)
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (66)
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    vue 설치
    jsp include
    mysql db
    자바 생성자
    러스트
    자바 클래스
    SSL 인증서
    Java Array
    VS Code 팁
    lang rust
    vscode
    jsp parameter
    rust linux
    css perspective
    자바 배열
    자바 exception
    css tb
    React Compiler
    에디터 팁
    vue cli
    Rust lang
    css transition
    Extension Bisect
    제미나이
    자바 객체 지향
    nginx ssl 적용
    servlet class
    gitlab 설치
    자바 for문
    nginx ssl 설정
    브라우저 자동화
    Node
    java casting
    svelte
    프런트엔드
    API 설계
    css table
    AI 반도체
    spring java
    AI 코딩 에이전트
    docker
    클론코딩
    nginx
    리눅스 설치
    오류
    ssl 인증서 발급
    ubuntu
    css float
    proxy pass
    티스토리 자동화
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[JS] 07. Array
상단으로

티스토리툴바