[Day4] JS 4 [11/23] 

 

js ๋ฌธ์ž์—ด : ๊ฒ€์ƒ‰

 

๋ฌธ์ž์—ด ์ผ์น˜

   let str = "์•ˆ๋…•ํ•˜์„ธ์š”, ํ™๊ธธ๋™ ์ž…๋‹ˆ๋‹ค. - ํ™๊ธธ๋™ ์™ˆ -";
  
   console.log( str.indexOf("ํ™๊ธธ๋™", 0) );  // 7
   console.log( str.indexOf("ํ™๊ธธ๋™") );       // 7
  
   //console.log( str.lastIndexOf("ํ™๊ธธ๋™", from)  );       // 7
   console.log( str.lastIndexOf("ํ™๊ธธ๋™")  );       // 18
  
   // ์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†๋Š” ๊ฒฝ์šฐ :   -1 ๋ฐ˜ํ™˜
  
   // indexOf(), lastIndexOf()์™€ search()์˜ ์ฐจ์ด์  : ์ •๊ทœํ‘œํ˜„์‹
   console.log(  str.search(/ํ™๊ธธ๋™/) );  // 7
  
   console.log( str.match("๊น€๊ธฐ์ˆ˜") );  // ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋Š” ๊ฒฝ์šฐ : null ๋ฐ˜ํ™˜
   console.log( str.match(/ํ™๊ธธ๋™/ig) );   
   // alert(  typeof str.match(/ํ™๊ธธ๋™/ig) );   // object   ๋ฐฐ์—ด  
  
   // ES 2020 ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ 
   //matchAll() ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐ˜๋ณต์ž(iterator)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
   const iterator = str.matchAll( /ํ™๊ธธ๋™/g );
   // ์ž๋ฐ” : console.log(  Arrays.toString( ๋ฐฐ์—ด๋ช… )  )
   console.log(   Array.from( iterator ) );
  
   
   // ES6 ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ 
   // IE ์ง€์›๋˜์ง€ ์•Š๋Š”๋‹ค.  
   // ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ boolean ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜.
   console.log(  str.includes("ํ™๊ธธ๋™") );  // true
   
   
   var url = "www.sist.co.kr";
   
   // ES6 ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ 
   // http://  ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘
   //   ??       ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋‹ˆ
   console.log(  url.startsWith( "http://") );		    // false
   console.log(  url.endsWith( "co.kr") );		     // true

์ด์Šค์ผ€์ดํ”„๋ฌธ์ž

    let text = "he\'s ~~";
   //let text = 'he\'s ~~';   // ๋ฌธ์ž์—ด ๋‚ด๋ถ€์—  ' ํ™‘๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ๋œ๋‹ค๋ฉด

-๋ฌธ์ž์—ด ๋ณ€์ˆ˜ ์ค‘๊ฐ„์— ๋“ค์–ด๊ฐ€๊ฒŒ-

    var name = "ํ™๊ธธ๋™";
    var age = 20;
    
    //                  ์ด๋ฆ„์€ 'ํ™๊ธธ๋™'์ด๊ณ , ๋‚˜์ด๋Š” 20์‚ด์ด๋‹ค.    
    // var result = "์ด๋ฆ„์€ '"+name+"'์ด๊ณ , ๋‚˜์ด๋Š” "+age+"์‚ด์ด๋‹ค.";
    // Java        String.format(   "์ด๋ฆ„์€ '%s'์ด๊ณ , ๋‚˜์ด๋Š” %d์‚ด์ด๋‹ค."     , name, age);
    //                 printf( format, name, age);
    
    // js ์—์„œ๋„ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„ค์š”... 
    //        `            ๋ฐฑํ‹ฑ๋ฌธ์ž
    var result = ` > ์ด๋ฆ„์€ '${ name }'์ด๊ณ , ๋‚˜์ด๋Š” ${ age }์‚ด์ด๋‹ค. `;
    // console.log( result );

-foreach-

   let header = "TEST";   
   let html = `<h3>${ header}</h3>`;
   
   let tags = ["javascript", "jquery", "java"];  // js ๋ฐฐ์—ด            
   // let tags = {};  // js ๊ฐ์ฒด
   
   // java  foreach๋ฌธ
   // js forin ๋ฌธ
   // js forof ๋ฌธ
   for (let tag of tags) {
	  html +=  `<li>${ tag }</li>`;
   } // for
   
   $("#demo").html(  html );

js ์ˆซ์ž (Number)

- js ์ˆซ์ž -                     (  X   int, short, long,   float, double )

- ์ •์ˆ˜, ์‹ค์ˆ˜ ๊ตฌ๋ถ„์—†์ด ์ €์žฅ. - number ํƒ€์ž…

- js ๋Š” ํ•ญ์ƒ 64๋น„ํŠธ ์ €์žฅ 

    var n = 3;
    var n = 3.14;
    var n = 3e+5;
    
    // 3 + 5     8
    // "3" + 5   "35"
    //  3 + "5"   "35"
    //  1 + 3 + "5"   "45"
    //  ์ˆซ์ž => ๋ฌธ์ž์—ด ํ˜•๋ณ€ํ™˜
    //   7 + ""            "7"
    
    
    //  + ๋ง์…ˆ, ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์—ฐ์‚ฐ์ž. 
    //  + ์ œ์™ธํ•œ  ์ˆซ์ž ๊ด€๋ จ ์—ฐ์‚ฐ์ž๋Š” ์ž๋™์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 
    var a = "100";
    var b = "10";
    console.log(  a - b );   // 100/10   =  10
    console.log(  a * b );   // 100/10   =  10
    console.log(  a / b );   // 100/10   =  10

-NaN (not a number) ๋ฆฌํ„ฐ๋Ÿด-

    // console.log(   100 / "apple"   );   // NaN ( Not a Number) ๋ฆฌํ„ฐ๋Ÿด
    // console.log( typeof NaN );  // number
    
    console.log(   100 / 0   );  // Infinity
    console.log(   100 % 0   ); // NaN
    
    
    // isNaN()
    
    console.log(  isNaN( 100 % 0 ));  //  true
    console.log(  isNaN( "100" ));  //  false
    console.log(  isNaN( "100a" ));  // true
    
    // [๋ฌธ์ œ]  ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์ด ์ˆซ์ž๋งŒ ์ž…๋ ฅํ–ˆ๋Š” ์ฒดํฌํ•˜๊ณ  ์‹ถ๋‹ค  -  isNaN()
    
    console.log(   NaN + 5  );    // NaN
    console.log(   NaN + "5"  );  // "NaN5"

-์ง„์ˆ˜-

  // js์—์„œ    Ox๋ฅผ ๋ถ™์ด๋ฉด 16์ง„์ˆ˜๋กœ ํ•ด์„ํ•œ๋‹ค. 
   let x = 0xFF;  
   // js์—์„œ    O๋ฅผ ๋ถ™์ด๋ฉด 8์ง„์ˆ˜๋กœ ํ•ด์„ํ•œ๋‹ค.
   let y = 07;
 
   let z = 10;
   console.log(   z.toString(2)  );  // "1010"
   console.log(   z.toString(8)  );  // "12"
   console.log(   z.toString(16)  );  // "a"

-์ˆซ์ž ๊ฐ์ฒด- ์„ฑ๋Šฅ X

    // var num = 123;                              // number
    var num = new Number(123);           // object               X    ์„ฑ๋Šฅ ๋–จ์–ด์ง„๋‹ค. 
    
    // Java
    //String name = "ํ™๊ธธ๋™";
    //String name = new String("ํ™๊ธธ๋™");

-64๋น„ํŠธ ์ˆซ์ž-

   // js  ์ˆซ์ž  : 64๋น„ํŠธ
   let xx = BigInt(์ˆซ์ž);

js ์ˆซ์ž ํ•จ์ˆ˜

 

    1. toString()                   7.toString()  "7"

   var n = 7 + "";    // "7"
   var m = 10;
   m.toString();      // "10"

    2. toExponential()          ๋ฐ˜์˜ฌ๋ฆผ๋˜๊ณ  + ์ง€์ˆ˜ ํ‘œ๊ธฐ๋ฒ• + "" ๋ฌธ์ž์—ด ๋ณ€ํ™˜

   // toExponential()  ๋ฐ˜์˜ฌ๋ฆผ + ์ง€์ˆ˜ํ‘œํ˜„ + ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜.
   var n = 9.656;
   // 2,4,6 ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์†Œ์ˆ˜์  ๋’ค์˜ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. 
   // ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ˜์˜ฌ๋ฆผํ•˜์ง€ ์•Š๋Š”๋‹ค. 
   //console.log(   n.toExponential(2)  ); // 9.66e+0
   //console.log(   n.toExponential(4)  ); // 9.6560e+0
   //console.log(   n.toExponential(6)  ); // 9.656000e+0

    3. toFixed()                    ๋ฐ˜์˜ฌ๋ฆผ + "" ๋ฌธ์ž์—ด ๋ณ€ํ™˜

    console.log(  n.toFixed(0) );  // 10
    console.log(  n.toFixed(2) );  // 9.66      ์ง€์ˆ˜ํ‘œํ˜„ X
    console.log(  n.toFixed(4) );  // 9.6560  ์ง€์ˆ˜ํ‘œํ˜„ X
    console.log(  n.toFixed(6) ); // 9.656000

    4. toPrecision()               ์ง€์ •๋œ ๊ธธ์ด๋กœ ์ž‘์„ฑ๋œ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜.

    console.log(  n.toPrecision() );   // 9.656
    console.log(  n.toPrecision(2) );  // 9.7
    console.log(  n.toPrecision(4) );  // 9.656
    console.log(  n.toPrecision(6) );  // 9.65600

    5. valueOf()                    X  ์ˆซ์ž ๋ณ€ํ™˜

    6. Number()

   // Number() ๋ฉ”์„œ๋“œ  :   js ๋ณ€์ˆ˜ -> ์ˆซ์ž ๋ณ€ํ™˜
   //                                 ์ˆซ์ž ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์œผ๋ฉด NaN ๋ฐ˜ํ™˜.
   //                                   ๋‚ ์งœ๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. 
   console.log(    Number(true)  );   // boolean(true)  ->   1
   console.log(    Number(false)  );  //                 false ->  0
   
   console.log(    Number("10")  );  //                 "10" ->  10
   console.log(    Number("10.33")  );  //             "10.33" ->  10.33
      
   console.log(    Number("          10")  );  //             "          10" ->  10
   console.log(    Number("10          ")  );  //             "10          " ->  10
   
   console.log(    Number("admin")  );  // NaN
   
   console.log(    Number("10 33")  );  // NaN
   console.log(    Number("10,33")  );  // NaN
   
   //                                   ๋‚ ์งœ๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. 
   console.log(    Number("2022-01-22")  );  // NaN
   
   // 1970.1.1 ์ดํ›„   ms๊ฐ’์„ ๋ฐ˜ํ™˜
   console.log(    Number( new Date( "2022-01-22") ) );  // 1642809600000

    7. parseInt()

   // parseInt() ๋ฉ”์„œ๋“œ  :  ๋ฌธ์ž์—ด์„ ๊ตฌ๋ฌธ ๋ถ„์„ํ•ด์„œ       ์ •์ˆ˜ ๋ฐ˜ํ™˜
   console.log( parseInt( "10"  )  );  // "10"     -> 10
   console.log( parseInt( "-10"  )  );  // "-10" -> -10
   console.log( parseInt( "10.33"  )  );  // "10.33" -> 10
   
   // Number()  NaN
   console.log( parseInt( "10 33"  )  );  // "10 33" -> 10
   console.log( parseInt( "10,33"  )  );  // "10,33" -> 10
   console.log( parseInt( "10abcde"  )  );  // "10adbcde" -> 10
   console.log( parseInt( "year 100"  )  );  // "year 100"-> NaN
   console.log( parseInt( "100 year"  )  );  // "year 100"-> 100

    8. parseFloat()

   // parseFloat() ๋ฉ”์„œ๋“œ  :  ๋ฌธ์ž์—ด์„ ๊ตฌ๋ฌธ ๋ถ„์„ํ•ด์„œ      ์ˆซ์ž( ์ •์ˆ˜ , ์‹ค์ˆ˜ ) ๋ฐ˜ํ™˜
   console.log( parseFloat( "10"  )  );  // "10"     -> 10
   console.log( parseFloat( "-10"  )  );  // "-10" -> -10
   console.log( parseFloat( "10.33"  )  );  // "10.33" -> 10.33
   
   // Number()  NaN
   console.log( parseFloat( "10 33"  )  );  // "10 33" -> 10
   console.log( parseFloat( "10,33"  )  );  // "10,33" -> 10
   console.log( parseFloat( "10abcde"  )  );  // "10adbcde" -> 10
   console.log( parseFloat( "year 100"  )  );  // "year 100"-> NaN
   console.log( parseFloat( "100 year"  )  );  // "year 100"-> 100

    9. isInteger()

    var n = 10;
    console.log(    Number.isInteger(  n )  );  // true
    n = 10.33;
    console.log(    Number.isInteger(  n )  );   // false

js ๋ฐฐ์—ด

  1. ๋ฐฐ์—ด ๊ฐœ๋…

  2. ๋ฐฐ์—ด ์‚ฌ์šฉ ๋ชฉ์ 

  3. ๋ฐฐ์—ด ์„ ์–ธ ํ˜•์‹ X

     

     [js]  ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

     var         cars = [];
     cars[0] = "BMW";
     cars[1] = "KIA";
     const  cars = [
                                  "BMW"
                                  , "KIA"
                                  , "Volvo"
                            ];

 

     [js]  ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

     const cars = new Array();     
     const cars = new Array("BMW",  , "KIA"   , "Volvo");

-๋ฐฐ์—ด์š”์†Œ ์ ‘๊ทผ-

     // int [] m = {1,3,5,4,2};
     // m[2]
     // m[2] = 100;
     
     const m = [1,3,5,4,2];
     
     // (1)   2๋ฒˆ์งธ ์š”์†Œ... 
     // console.log(  m[2] );  // 5
     
     // (2)   2๋ฒˆ์งธ ์š”์†Œ์˜ ๊ฐ’์„ 100์œผ๋กœ ๋ณ€๊ฒฝ
     m[2] = 100;
     console.log(  m[2] );  //  100
     
     // (3) ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅ.
     // console.log(  m );  // [1, 3, 100, 4, 2]
     
     // (4) ๋ฐฐ์—ด์˜ ํƒ€์ž…
     //  js์—์„œ ๋ฐฐ์—ด๋„ ๊ฐ์ฒด๋กœ ์ธ์‹ํ•œ๋‹ค. 
     console.log( typeof m );  // object
     
     // (5) ์š”์†Œ ์ถ”๊ฐ€ 55
     // ๋ฐฐ์—ด๋ช….length   == ๋ฐฐ์—ดํฌ๊ธฐ
     m[m.length] = 55;
     m[m.length] = 65;
     m[m.length] = 75;
     console.log(  m ); 
     
     // (7) ๋ฐฐ์—ด ์ •๋ ฌ
     m.sort();
     console.log(  m );  // 1, 100, 2,3,4,5  ๋ฌธ์ž์—ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
     
     // (8) ๋งˆ์ง€๋ง‰ ์š”์†Œ ์–ป์–ด์˜ค๊ธฐ.
     // m[m.length-1]
     
     // (9) ๋ชจ๋“  ๋ฐฐ์—ด์˜ ์š”์†Œ ์ถœ๋ ฅ.
     for (var i = 0; i < m.length; i++) {
		console.log(  i +" : " +     m[i] );
	 }
     
     // (10)
     m[m.length] = "Benz";  // ๋ฌธ์ž
     m[m.length] = Date.now;  // ๋‚ ์งœ
     m[m.length] = function (){ }; // ํ•จ์ˆ˜
     m[m.length] = [1,2,3];  // ๋ฐฐ์—ด

-๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด ์ฐจ์ด-

    const person = ["admin", 20];
    var name = person[0];
    var age = person[1];
    
    // js object
    const person = {
    		name:"admin" , 
    		age : 20
    };
    person.name;
    person["name"];

 

๋ฐฐ์—ด ๋ฉ”์„œ๋“œ

- ๊ธฐ๋Šฅ, ๋ฆฌํ„ด๊ฐ’, ๋งค๊ฐœ๋ณ€์ˆ˜ ( 3๊ฐ€์ง€ )

 

-๋ฐฐ์—ดํฌ๊ธฐ, foreach, push, isArray-

   var fruits = ["Banana", "Orange", "Apple", "Mango"];
   // console.log(  fruits.length );  // ๋ฐฐ์—ด ํฌ๊ธฐ  4
   
   /*  [1๋ฒˆ]
   var text = "<ul>";
    for (var i = 0; i < fruits.length; i++) {
		text += "<li>" + fruits[i] + "</li>";
	}   // for
	text += "</ul>";
	*/
	
	// [2๋ฒˆ]  Array.forEach()
	var text = "<ol>";
	fruits.forEach(function(elt, i, array) {
		// console.log(  elt + " / " + i + " / " + array)
		text += "<li>" + elt + "</li>";
	})
	text += "</ol>";
	
	$("#demo").html( text );
	
	// 1.  ๋ฐฐ์—ด ์š”์†Œ ์ถ”๊ฐ€  :   push()
	// fruits[fruits.length] = "xxx";             X
	fruits.push("Lemon");
	
	// 2. ํƒ€์ž…์ด ๋ฐฐ์—ด ํ™•์ธ : ES5   Array.isArray()
	// console.log( typeof fruits );  // object
	// console.log(  Array.isArray( fruits ) );  // true
	
	// instanceof ์—ฐ์‚ฐ์ž.
	// console.log( " > " +   fruits  instanceof Array );  // true

-toString, join-

  // [ ๋ฐฐ์—ด -> ๋ฌธ์ž์—ด ๋ณ€ํ™˜        toString() ]  
  //  ์ฒซ ๋ฒˆ์งธ.
  //   "Banana,Orange,Apple,Mango,Lemon" 
  // ๊ฐ ์š”์†Œ๊ฐ€ ์ฝค๋งˆ(์‰ผํ‘œ)๋กœ ๊ตฌ๋ถ„๋˜์–ด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜.
  // console.log(   fruits.toString() );
  
  //  ๋‘ ๋ฒˆ์งธ   - join()   ***
  //  "Banana - Orange - Apple - Mango - Lemon"      seperator (๊ตฌ๋ถ„์ž)
  // console.log(  fruits.join(" - ") );
  
  // Banana</li><li>Orange</li><li>Apple</li><li>Mango</li><li>Lemon
  // console.log(  fruits.join("</li><li>") );
  
   var text = "<ol><li>";
	text += fruits.join("</li><li>");
	text += "</li></ol>";
	
	$("#demo").html( text );

-push, pop-

   // ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ - ๋ฆฌํ„ด๊ฐ’  ๋ฐฐ์—ด์˜ ํฌ๊ธฐ    push()
   console.log(  fruits.push("XXX") );  // 6
   console.log(  fruits.toString() );
   
   // ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์–ป์–ด์˜ค๊ธฐ   - pop()           + ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ.
   // console.log(  fruits[fruits.length - 1] );
   console.log( fruits.pop() );
   console.log(  fruits.toString() );
   
   sort()
   shift()
   unshift()
   concat()
   splice()

-shift, delete- 

    var m = [3,5,2,4,1];
    console.log(  m.toString() );
    
    // [ shift() ]   >>     [<<1 ]   >>>          ์ž๋ฆฌ ์ด๋™
    // ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์ œ๊ฑฐ๋˜๊ณ  ๋‚˜๋จธ์ง€ ์š”์†Œ๋Š” ์ธ๋ฑ์Šค -1    ์ด๋™
    // 0[3] 1[5] ~  4[1]
    // 0[5] ~  3[1]
    // console.log(  m.shift() );   // 3  ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    
    
    
    // [ unshift() ]
    // m.push(100)  ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ถ”๊ฐ€
    // m.unshift(100);  // ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ์ถ”๊ฐ€ + ์š”์†Œ ๋’ค๋กœ ์ธ๋ฑ์Šค + 1  ์ด๋™
    
    
    // ๋ฐฐ์—ด ์š”์†Œ ์ œ๊ฑฐ(์‚ญ์ œ)
    // shift()  ์ฒซ ๋ฒˆ์งธ       ์š”์†Œ ์ œ๊ฑฐ
    // pop()    ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ
    // m[1] = -1;   ์ œ๊ฑฐํ–ˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ ํŠน์ •๊ฐ’์„ ์ˆ˜์ •.
    
    // 3,   undefined  ,2,4,1
    // delete m[1];
    
    var n = [100,200,300];
    var s = [7,8,9];
    // 4. m ๋ฐฐ์—ด๊ณผ n ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉ(์—ฐ๊ฒฐ)ํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ :    concat()
    // 3,5,2,4,1,100,200,300
    var mns =  m.concat(n, s);
    
    // console.log(  mns.toString() );

-splice-

  // 3,5                         ,2,4,1
  //      2(100),3(200) 
  // 3,5,100,200,2,4,1
  // ๋ฐฐ์—ด์˜ ์ƒˆ ํ•ญ๋ชฉ์„ ์‚ฝ์ž…
  // unshift() : ์ฒซ ์š”์†Œ ์ถ”๊ฐ€
  // push()     : ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ถ”๊ฐ€
  // splice()              ์ค‘๊ฐ„ ์š”์†Œ  1๊ฐœ ์ด์ƒ ์ถ”๊ฐ€(์‚ฝ์ž…)
  // ์Šคํ”Œ๋ผ์ด์Šค() == ์‚ฌ์ „์ ์ธ ์˜๋ฏธ ( ์ž‡๋‹ค , ๋ถ™์ด๋‹ค. )
   // pos  ์‚ฝ์ž… ์œ„์น˜
   // amount  ์ œ๊ฑฐ ์–‘
   // newelt    ์ƒˆ๋กœ์šด ์š”์†Œ
   m.splice(pos, amount, newelt)
  
  // 3,5,100,200,2,4,1
   m.splice(2, 0, 100, 200);
  
  //  3,5,2,4,1
  // 3,5,100,200,1
   m.splice(2, 2, 100, 200);
  
  //  3,5,[2,4],1    2๋ฒˆ์งธ ๋ถ€ํ„ฐ 2๊ฐœ์˜ ์š”์†Œ ์ œ๊ฑฐ
  // 3,5,1
   m.splice(2, 2);
  
   console.log( m.toString() );

-slice, splice-

    console.log( m );
    
    // m.splice()    ์ž‡๋‹ค.
    // m.slice()      ์กฐ๊ฐ๋‚ด๋‹ค.       
    // var n = m.slice(1, 3); // 1 <=     < 3
    var n = m.slice(1); // 1 <=     to ๋งˆ์ง€๋ง‰ ์š”์†Œ
    console.log( n ) ;   // [5,2]

   sort() ๊ธฐ๋ณธ asc

   reverse() ์š”์†Œ ์ˆœ์„œ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜

   let fruits = ["Banana", "Orange", "Apple", "Mango"];
   
   // "Banana,Orange,Apple,Mango"
   console.log( fruits.toString() );
   // 1. ๋ฐฐ์—ด ์ •๋ ฌ + asc
   // fruits.sort();
   
   // 2. ๋ฐฐ์—ด ์ •๋ ฌ + desc
   // fruits.sort(function(a, b) {  })
   
   // ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜
   // "Mango,Apple,Orange,Banana"
   // "Orange,Mango,Banana,Apple"  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
   fruits.sort()
   fruits.reverse();
   
   // [A]pple,[B]anana,[M]ango,[O]range  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
   console.log( fruits.toString() );
   // ์ˆซ์ž ๋ฐฐ์—ด ์„ ์–ธ
   var m = [3, 5, 10, 2, 4, 1 , 29];
   console.log( m.toString() ) ;  // 3,5,10,2,4,1,29
   
   // m.sort();  ์ด์œ ? ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ •๋ ฌ..
   //  1,2,3,4,5,10,29		     ์˜ค๋ฆ„์ฐจ์ˆœ
   /*
   m.sort(function(a, b) {
   	  // return a - b;   // ์–‘์ˆ˜,  ์Œ์ˆ˜ , 0   ์˜ค๋ฆ„์ฐจ์ˆœ
   	  return b - a;  // ๋‚ด๋ฆผ์ฐจ์ˆœ
   });
   */
   
   // ๋žŒ๋‹ค์‹
   m.sort(  (a, b) => a-b  );
   
   console.log( m.toString() ) ;  // "1","10","2","29","3","4","5"

-๋ฐฐ์—ด ์„ž๊ธฐ(Math.random)

   // ๋ฌด์ž‘์œ„๋กœ ๋ฐฐ์—ด ์„ž๊ธฐ.
   // js       0.0 <=    Math.random() ์‹ค์ˆ˜  < 1
   m.sort(  () => 0.5 - Math.random()  );
   console.log( m.toString() ) ;

- js : Math ๊ฐ์ฒด + random() ๋ฉ”์„œ๋“œ

   var max =  Math.max(...m)  ;  // 29
   
   // [ apply() ํ•จ์ˆ˜  ? ]  call() ๋น„๊ตํ•ด์„œ ์„ค๋ช…...
   max = Math.max.apply(null, m); // 29
   console.log( max );
   
   var min = Math.min(...m)  ;  // 1
   min = Math.min.apply(null, m); 
   console.log( min );

-๊ฐ์ฒด ๋ฐฐ์—ด-

   // ๊ฐ์ฒด ๋ฐฐ์—ด ์„ ์–ธ
   var persons = [  
	                      {name:"ํ™๊ธธ๋™", age:20}, 
	                      {name:"๋ฐ•์˜ˆ๋ฆฐ",age:25}, 
	                      {name:"์ด์˜ˆ์ธ",age:18} 
	                 ];
   
   // 1. ๋‚˜์ด ์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅ.
   //console.log(   persons[0].age   );
   //console.log(   persons[0]["age"]   );
   
   persons.sort(function( o1, o2) {
   	   return o1.age - o2.age;
   })
   
   // console.log( persons.toString() );
   // console.log( persons );
   
   // 2. ์ด๋ฆ„์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ..
   console.log("===2===");
   
   
   //console.log(    "์ด์˜ˆ์ธ" - "ํ™๊ธธ๋™"  );  // NaN
   //console.log(    "์ด์˜ˆ์ธ" > "ํ™๊ธธ๋™"  );  // false
   //console.log(   Number(  "์ด์˜ˆ์ธ" > "ํ™๊ธธ๋™" ) );  // false
   
    persons.sort(function( o1, o2) {
       if( o1.name > o2.name ) return 1;
       else if( o1.name < o2.name ) return -1;
       else 0;
   })
   
   console.log( persons );

๋ฐฐ์—ด ๋ฉ”์„œ๋“œ

  1. forEach()

     ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ ๋งˆ๋‹ค  ํ•œ ๋ฒˆ์”ฉ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ.

     

  2. map()

     ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜   

  

  3. filter()

      ํ•„ํ„ฐ๋ง๋œ ์š”์†Œ๋งŒ์„ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜   

      

  4. reduce()

       ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์—์„œ ๋‹จ์ผ ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜    

       

   5. every()

       ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜    

       

   6. some()

       ๋ฐฐ์—ด์˜ ์ผ๋ถ€ ์š”์†Œ๋ผ๋„ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋ฉด true/false ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜    

       

   7. indexOf()    / lastIndexOf()

       ๋ฐฐ์—ด์—์„œ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•œ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜  ( - 1 )

       

   8.  find()  / findIndex()

        ์š”์†Œ๊ฐ’      ์š”์†Œ ์ธ๋ฑ์Šค

        5                 1

        var m = [3,5,2,4,1];

        ์ฒซ ๋ฒˆ์งธ 5๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ์ˆ˜๋ฅผ ์ฐพ๊ณ  ์‹ถ๋‹ค.

        

        var num = m.find( function (elt, i, array){

           return elt >=5;

        } )

     

     9. from()     

             ๊ธธ์ด(length) ์†์„ฑ์ด ์žˆ๋Š” ๊ฐ์ฒด ๋˜๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์—์„œ -> ๋ฐฐ์—ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

             ๋ฌธ์ž์—ด

             ๋ฐฐ์—ด

             ๋“ฑ๋“ฑ   

             

      10. keys() 

          ๋ฐฐ์—ด์˜ ํ‚ค(์ธ๋ฑ์Šค๊ฐ’)๊ฐ€ ์žˆ๋Š” iterator ๊ฐ์ฒด๋ฅผ  ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

          

          entries()    key                        +   value ํ•œ์Œ

                        ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๊ฐ’                ์š”์†Œ์˜ ๊ฐ’

                        [ 0 , 3 ]

<script>
   var m = [3,5,2,4,1];
   // var ir = m.keys();
   var ir = m.entries();  // ์—”ํŠธ๋ฆฌ = key + value  ํ•œ์Œ
   
   for (let n of ir) {  // index   0/1/2/3/4   ์ธ๋ฑ์Šค๊ฐ’ 
	console.log( n[1] );
  }
   
   /* JAVA
   while( ir.hasNext()){
	    int n = ir.next();
   }
   */
	   
</script>


<script>
        /*
        var str = "์•ˆ๋…•ํ•˜์„ธ์š”.";
        // console.log( str.length );  // 6
        var arr =  Array.from( str );
        console.log( typeof arr);  // object
        console.log( arr.toString()  );  // "์•ˆ,๋…•,ํ•˜,์„ธ,์š”,."
        */
</script>

<script>
   /*
   var m = [3,5,2,4,1];
   // 1. forEach()
   m.forEach(function(elt, i, array) {
          console.log( elt + " / " +  i ); 	
    });
   */
</script>

<script>
    /*
    // 2. map()
    var m = [3,5,2,4,1];
    console.log( m.toString() );            // 3,5,2,4,1
    
    // ๊ฐ ์š”์†Œ์˜ ๊ฐ’์„  2๋ฅผ ๊ณฑํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ.
  
    var newm = [];
    for (var i = 0; i < m.length; i++) {
    	newm.push(    m[i] * 2 );
	}; 
 
	
	// map() ์‚ฌ์šฉ
	var newm = m.map(function(elt, i, array) {
		return elt *3 ;
	});
	
	console.log(   newm.toString() );  // 9,15,6,12,3
	*/
</script>

<script>
   /*
   // 3. filter()
   var m = [];
   for (var i = 0; i < 10; i++) {
	    m.push(   parseInt(  Math.random() * 100 )  );
  }
   console.log( m.toString() );
   
   // 50๋ณด๋‹ค ํฐ ๊ฐ’์„ -> ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
 
   var newm = [];
   m.forEach(function(elt, i, array) {
   	     if(  elt >= 50  )   newm.push( elt );
   })
   console.log( newm.toString() );
 
   
   var newm = m.filter(function(elt, i, array) {
   	   return elt >= 50 ;
   });
   console.log( newm.toString() );
   */
</script>

<script>
    /*
   // 4. reduce()
   var m = [3,5,2,4,1];
   
   // ๋ฐฐ์—ด์˜ ์ดํ•ฉ์„ ๊ตฌํ•ด์„œ ์ถœ๋ ฅ.
 
   var sum = 0;
   
   for (var i = 0; i < m.length; i++) {
	  sum += m[i];
	  console.log(  m[i] +"+" )
  }
   console.log( sum );
 
   
   // m.reduceRight( )               ์˜ค๋ฅธ์ชฝ -> ์™ผ์ชฝ
   var sum = m.reduce(function(sum, elt, i, array) {
   	   return sum + elt;
   } )
    console.log( sum );  // 115
    */
</script>

<script>
   // ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ ์‹œํ—˜
   var m = [78, 60, 35, 89, 76];
   // ๊ณผ๋ฝ. ์ฒดํฌ
   /*
   for (var i = 0; i < m.length; i++) {
	   if(  m[i]  < 40 ){  // 40 ๋ฏธ๋งŒ 
		    console.log("๊ณผ๋ฝ")
	   }
  }
   */
   
  var result =   m.every(function(elt, i, array) {
   	   return elt >=40 ;
   })
   
   console.log( result  ); // false

</script>

arguments ์ปฌ๋ ‰์…˜,  js ๋‚ ์งœ

 [  js  ๋‚ ์งœ ๊ฐ์ฒด :  Date ]

 1. ์ƒˆ๋กœ์šด ๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•

    (1)  new Date() :  ํ˜„์žฌ ์‹œ์Šคํ…œ์˜ ๋‚ ์งœ + ์‹œ๊ฐ„.

    (2)  new Date(y, m, d, h, m, s, ms)

    (3)  new Date( ms )                  1970.1.1 00:00:00.000

    (4)  new Date( string date )

  //  [  js  ๋‚ ์งœ ๊ฐ์ฒด :  Date ]
  //  (1)  new Date() :  ํ˜„์žฌ ์‹œ์Šคํ…œ์˜ ๋‚ ์งœ + ์‹œ๊ฐ„.
      var now = new Date();
      // Wed Nov 23 2022 16:12:30 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
      $("#demo").html(  now  );
  
  //  (2)  new Date(y, m, d, h, m, s, ms)
        // var d = new Date(2018 , 11-1, 23);  // 11์›”
        var d = new Date(2018 , 11-1 );          // ๋‚ ์งœ ์ƒ๋žต    1์ผ
        $("#demo").html( d   );
      
  //  (3)  new Date( ms )                  1970.1.1 00:00:00.000
       var ms =   Number(new Date());
       // console.log( ms );   1669187859080
       var d = new Date( ms );
       
       // Wed Nov 23 2022 16:18:18 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
       $("#demo").html( d   );
  
  //  (4)  new Date( string date )
       Sun Nov 25 2018 09:00:00 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
       var d = new Date("2018-11-25");
       var d = new Date("2018-11");  // 1์ผ
       var d = new Date("2018");  // 1์›” 1์ผ
       $("#demo").html( d   );
       
       // GMT  == ๊ทธ๋ฆฌ๋‹ˆ์น˜ ํ‘œ์ค€์‹œ
       // UTC   == ํ˜‘์ • ์„ธ๊ณ„์‹œ
  
        // MM/DD/YYYY
        // Wed Mar 25 2015 00:00:00 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
       var d = new Date("03/25/2015");       
       var d = Date.parse("03/25/2015");
       
       d.toString() ; //   ๋‚ ์งœ -> ๋ฌธ์ž์—ด ๋ณ€ํ™˜
       
       $("#demo").html( d.toDateString()   );  // "Wed Mar 25 2015"
       $("#demo").html( d.toUTCString()  );  // "Tue, 24 Mar 2015 15:00:00 GMT"
       $("#demo").html( d.toISOString()  );  // "2015-03-24T15:00:00.000Z"
       $("#demo").html( d.toLocaleDateString() );  // 2015. 3. 25.
       $("#demo").html( d.toLocaleTimeString() );  // ์˜ค์ „ 12:00:00
  function sum(a, b, c){
	  console.log(" >> sum(3) ... " );
  }

  function sum(){
	   // forms ์ปฌ๋ ‰์…˜
	   // options ์ปฌ๋ ‰์…˜
	   // arguments ์ปฌ๋ ‰์…˜ : ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜) ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด
	   // ๋“ฑ๋“ฑ  links, images ์ปฌ๋ ‰์…˜ ... ~ 
	   
	   // console.log(  arguments.length );
        var result = 0;
        for (var i = 0; i < arguments.length; i++) {
			result += arguments[i];
		}
	   return result;
   } 

  
  // sum();
  console.log(  sum(1, 2)  );              // 3
  console.log(  sum(1, 2, 3) );           // 6
  console.log(  sum(1, 2, 3, 4)  );      // 10
<script>
    var now = new Date();
   // ๋…„๋„
   console.log(  now.getFullYear() );  // 2022
   // ์›”
   console.log( now.getMonth() + 1 );      // 11
   // ์ผ
   console.log( now.getDate() );      // 23
   // ์‹œ
    console.log( now.getHours() );    // 16
   // ๋ถ„
   console.log( now.getMinutes() );    // 16
   // ์ดˆ
   console.log( now.getSeconds() );    // 16
   // ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ
   console.log( now.getMilliseconds() );    // 16
   // ์š”์ผ
   console.log(  "์ผ์›”ํ™”์ˆ˜๋ชฉ๊ธˆํ† ".charAt( now.getDay()  ) );    //   ์ผ(0) 1(์›”)  2(ํ™”) 3(์ˆ˜์š”์ผ)    ~    6(ํ† )

   
   // ๋‚ ์งœ ์„ค์ •
   /* 
   now.setFullYear(year);
   now.setMonth(month)  // 1 ๋‹ฌ
   now.setDate(date);
   now.setHours(hour);
   now.setXXX();  
   */   
    
</script>

<script>
      //  getTime() :  1669188846578  ( 1970.1.1 00:00:00.000 )
     var ms =  now.getTime();
     alert( ms );
</script>

html - ๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ

<select   onchange="bc_change(  this  );">
   <option>์„ ํƒํ•˜์„ธ์š”.</option>
   <option value="white">ํฐ์ƒ‰</option>
   <option value="red">๋นจ๊ฐ•</option>
   <option value="green">๋…น์ƒ‰</option>
   <option value="blue">ํŒŒ๋ž‘์ƒ‰</option>
</select>
   function bc_change(  selTag ){
	   /* 
	  // document.querySelector("select");
	   var idx =  selTag.selectedIndex;  // 0 ~ 4
	   var op = selTag.options[idx];
	   var text =  op.text;
	   var value =  op.value;
	   */
	   
	   // alert(  selTag.value );    == $("select").val()
	   document.bgColor =   selTag.value;
   }
<script>
    var colors = ["white", "red", "green" , "blue"];

    var  $sel =  $("<select></select>");
    
    colors.forEach(function(elt, i, array) {
    	$sel.append(   $("<option>"+ elt +"</option>")  )
    });
    
    $sel.change(function() {
    	document.bgColor = $(this).val();
    	// $(this option:selected).val()
    });
    
    $sel.appendTo( $("body") );
  
</script>
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ