JS/JavaScript

[JS] 07. Array

밍글링글링 2017. 8. 28.
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

댓글