Array ์™€ Object

์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋ ค๋ฉด ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์› ๋˜ ๋ฐฉ์‹์œผ๋กœ๋Š” ์ด๋ ‡๊ฒŒ ํ•ด์•ผํ•œ๋‹ค.

var car = '์…€ํ† ์Šค';
var carPrice = 2700;
var carColor = 'white';

์„ธ๊ฐœ๋งŒ ํ•ด๋„ ๋ฒŒ์จ ๊ท€์ฐฎ์€๋ฐ ๋งŒ์•ฝ 100๊ฐœ๋ผ๋ฉด??

๋‹คํ–‰ํžˆ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

Array ์ž๋ฃŒํ˜•

var car = ['์…€ํ† ์Šค', 2700, 'white'];
    console.log(car);     //์ถœ๋ ฅ ๊ฒฐ๊ณผ ย ['์…€ํ† ์Šค', 2700, 'white']
    console.log(car[0]);  //์ถœ๋ ฅ ๊ฒฐ๊ณผ  '์…€ํ† ์Šค'

    //๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
    car[0] = 'ํŽ˜๋ผ๋ฆฌ';
    console.log(car[0]);  //์ถœ๋ ฅ ๊ฒฐ๊ณผ 'ํŽ˜๋ผ๋ฆฌ'
    
    //๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
    car[3] = '์—‰์ฐจ';   
    console.log(car[3]);  //์ถœ๋ ฅ ๊ฒฐ๊ณผ '์—‰์ฐจ' 

Object ์ž๋ฃŒํ˜•

var car2 = {name:'์…€ํ† ์Šค', price:2700, color:'white'};
    console.log(car2['name']);  //์ถœ๋ ฅ๊ฒฐ๊ณผ '์…€ํ† ์Šค'
    console.log(car2.price);    //์ถœ๋ ฅ๊ฒฐ๊ณผ 2700
    
    //๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
    car2.name = 'ํฌ๋ฅด์‰';
    //๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ 
    car2.option = '์—‰์ฐจ';
    console.log(car2.name, car2.option); //์ถœ๋ ฅ๊ฒฐ๊ณผ 'ํฌ๋ฅด์‰' , '์—‰์ฐจ'
    

Object๋Š” key: value ํ˜•ํƒœ๋กœ ์—ฌ๋Ÿฌ ์†์„ฑ์„ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…

Array ์™€ Object๋Š” ์ด๋ ‡๊ฒŒ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

var carCollection = [{name:'์…€ํ† ์Šค', price : 2700},
      {name:'ํฌ๋ฅด์‰',price: 10000},
      {name:'๋žŒ๋ณด๋ฅด๊ธฐ๋‹ˆ',price: 10000}]

var collection = {car : ['์…€ํ† ์Šค', 2700, 'white'],
               coin : ['๋Œ€ํ•œ๋ฏผ๊ตญ', 50]}


    console.log(carCollection);
    console.log(collection);

Array์™€ Object ์ฐจ์ด

  1. ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ฝ‘์•„์„œ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์ฐจ์ด

array๋Š” ์ €์žฅ ํ•  ๋•Œ์—๋Š” ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์ž๋ฃŒ๋ฅผ ๋ฝ‘์•„๋‚ผ ๋•Œ ๋ถˆํŽธํ•˜๋‹ค.

var car = ['์…€ํ† ์Šค', 2700, 'white', '์—‰์ฐจ', '์—‰๋œจ', 'HUD', '์ „๋ฐฉ์ถ”๋Œ๋ฐฉ์ง€'];

var car2 = {name:'์…€ํ† ์Šค', price:2700, color:'white', option1: '์—‰์ฐจ',
            option2: '์—‰๋œจ', option3: 'HUD',option4: '์ „๋ฐฉ์ถ”๋Œ๋ฐฉ์ง€'};

์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋Ÿฐ Array์™€ Object๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด

Array์—์„œ ์ƒ‰์ƒ์„ ๋ฝ‘์œผ๋ ค๋ฉด ์ƒ‰์ƒ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ช‡๋ฒˆ์งธ์— ์žˆ์—ˆ์ง€๋ฅผ ์ƒ๊ฐํ•ด์•ผํ•˜์ง€๋งŒ

Object์—์„œ๋Š” color ํ‚ค๊ฐ’๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ๋œ๋‹ค.

  1. ์ˆœ์„œ ๊ฐœ๋…

array์—๋Š” ์ˆœ์„œ ๊ฐœ๋…์ด ์žˆ๊ณ  object์—๋Š” ์—†๋‹ค.

array ์ž๋ฃŒ๋Š” ์ˆœ์„œ๊ฐœ๋…์ด ์žˆ๋‹ค๋ณด๋‹ˆ

  • ๊ฐ€๋‚˜๋‹ค ์ˆœ ์ •๋ ฌ

  • n๋ฒˆ ์ž๋ฃŒ๋ถ€ํ„ฐ n1๋ฒˆ ์ž๋ฃŒ๊นŒ์ง€ ์ž๋ฅด๊ธฐ

  • n๋ฒˆ ์ž๋ฃŒ ๋ฐ”๊พธ๊ธฐ

  • ๋งจ๋’ค๋‚˜ ๋งจ ์•ž์— ์ž๋ฃŒ ๋„ฃ๊ธฐ

  • ์›ํ•˜๋Š” ์ž๋ฃŒ๊ฐ€ ๋“ค์–ด์žˆ๋‚˜ ๊ฒ€์ƒ‰

์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง€๊ธˆ์€ ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค๋ผ๊ณ  ์•Œ๊ณ  ํ•„์š”ํ•  ๋•Œ ๊ฒ€์ƒ‰ํ•ด์„œ ์‚ฌ์šฉํ•ด๋ณด์ž.

โœจ ๊ณผ์ œ. js011.html์—์„œ ์ƒ์„ฑํ•œ car2์— ์ €์žฅ๋œ name๊ณผ price๋ฅผ ์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ ๋ถ€๋ถ„์— ๋„ฃ์–ด๋ณด์ž.

js011.html
<div class="container mt-3">
  <div class=" p-3">
    <span>์ƒํ’ˆ๋ช…</span>
    <span>๊ฐ€๊ฒฉ</span>
  </div>
</div>

carCollection์ด๋‚˜ collection ์ฒ˜๋Ÿผ ๋ณต์žกํ•œ ๊ตฌ์กฐ์— ๋ฐ์ดํ„ฐ๋Š” ์–ด๋–ป๊ฒŒ ๋ฝ‘์„ ์ˆ˜ ์žˆ์„๊นŒ?

var carCollection = [{name:'์…€ํ† ์Šค', price : 2700},
      {name:'ํฌ๋ฅด์‰',price: 10000},
      {name:'๋žŒ๋ณด๋ฅด๊ธฐ๋‹ˆ',price: 10000}]

    var collection = {car : ['์…€ํ† ์Šค', 2700, 'white']
                     ,coin : ['๋Œ€ํ•œ๋ฏผ๊ตญ', 50]}

console.log(carCollection[0].name);  //์ถœ๋ ฅ ๊ฒฐ๊ณผ '์…€ํ† ์Šค'
console.log(collection.coin[0]);     //์ถœ๋ ฅ ๊ฒฐ๊ณผ '๋Œ€ํ•œ๋ฏผ๊ตญ'

์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด๊ฒŒ ์–ด๋””์— ์“ฐ์ด๋Š”๊ฐ€?

์‡ผํ•‘๋ชฐ ์‚ฌ์ดํŠธ๋ฅผ ๋ณด๋ฉด์„œ ์–˜๊ธฐํ•ด๋ณด์ž.

โœจ๊ณผ์ œ. js013.html์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์€ ๋’ค ๊ฐ ์ƒํ’ˆ์˜ title ๊ณผ price๋ฅผ html์— ๋„ฃ์–ด์„œ ์™„์„ฑํ•ด๋ณด์ž. (๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ)

js013.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>

</head>
<body>
    <div class="card-group container">
        <div class="card">
            <img src="https://via.placeholder.com/600">
            <div class="card-body">
                <h5>Card title</h5>
                <p>๊ฐ€๊ฒฉ : 70000</p>
                <button class="btn btn-danger">์ฃผ๋ฌธํ•˜๊ธฐ</button>
            </div>
        </div>
        <div class="card">
            <img src="https://via.placeholder.com/600">
            <div class="card-body">
                <h5>Card title</h5>
                <p>๊ฐ€๊ฒฉ : 70000</p>
                <button class="btn btn-danger">์ฃผ๋ฌธํ•˜๊ธฐ</button>
            </div>
        </div>
        <div class="card">
            <img src="https://via.placeholder.com/600">
            <div class="card-body">
                <h5>Card title</h5>
                <p>๊ฐ€๊ฒฉ : 70000</p>
                <button class="btn btn-danger">์ฃผ๋ฌธํ•˜๊ธฐ</button>
            </div>
        </div>
    </div>

    <script>
        var products = [
            { id : 0, price : 70000, title : 'Blossom Dress' },
            { id : 1, price : 50000, title : 'Springfield Shirt' },
            { id : 2, price : 60000, title : 'Black Monastery' }
        ];
    </script>
</body>
</html>

๋ฐ‘์— ๋ณด๊ณ  ๋”ฐ๋ผ์น˜์ง€ ๋ง๊ณ  ํ˜ผ์ž์„œ ํ•ด๋ณด์ž

 for(var i =0; i< products.length; i++){
            cardChange(i);
        }

function cardChange(num) {
    document.querySelectorAll('.card-body h5')[num].innerHTML = products[num].title;
    $('.card-body p').eq(num).html(products[num].price);
}

๐Ÿ’Žjavascript ๋ณ€์ˆ˜ ํ™œ์šฉ

์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ์—์„  ๋ง์…ˆ๊ธฐํ˜ธ๋กœ ๋ฌธ์ž๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

console.log(1+"3");      //'13' ์ถœ๋ ฅ ๋œ๋‹ค.

์ˆซ์ž + ์ˆซ์ž๋Š” ๋ง์…ˆ

์ˆซ์ž + ๋ฌธ์ž๋Š” ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

var a = 'ํ’€์Šคํƒ๊ฐœ๋ฐœ์ž';
console.log('๋ฌธ์ž' + a + '๋ฌธ์ž'); // '๋ฌธ์žํ’€์Šคํƒ๊ฐœ๋ฐœ์ž๋ฌธ์ž' ์ถœ๋ ฅ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฌธ์ž ์‚ฌ์ด์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ณ€์ˆ˜ ์–‘์˜†์— + ์“ฐ๋Š”๊ฒŒ ๊ท€์ฐฎ๋‹ค๋ฉด

console.log(`๋ฌธ์ž ${a} ๋ฌธ์ž`);// '๋ฌธ์ž ํ’€์Šคํƒ๊ฐœ๋ฐœ์ž ๋ฌธ์ž' ์ถœ๋ ฅ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฐฑํ‹ฑ ๊ธฐํ˜ธ ` ์‚ฌ์ด์— ๋ฌธ์ž์—ด๊ณผ ${๋ณ€์ˆ˜}๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

๋ฐฑํ‹ฑ๊ธฐํ˜ธ๋Š” ์ˆซ์ž1 ์™ผ์ชฝํ‚ค ~ ๋ฐ‘์— ์žˆ๋Š” ํ‚ค๋‹ค.

Last updated