Zoeeey 2023. 4. 13. 16:41

5. ์นด๋“œ๋ ˆ์ด์•„์›ƒ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์นด๋“œ 3๊ฐœ ์ƒ์„ฑํ•˜๊ธฐ

<div class="container">
    <div class="row">
    </div>
</div>

์›์‹œ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฒ„์ „

let cardWrap = document.querySelector('.row');
let products = [
    { id : 0, price : 70000, title : 'Blossom Dress' },
    { id : 1, price : 50000, title : 'Springfield Shirt' },
    { id : 2, price : 60000, title : 'Black Monastery' }
];

products.forEach(function(a, i){
    let card = `<div class="col-sm-4">
          <img src="https://via.placeholder.com/600" class="w-100">
          <h5>${products[i]['title']}</h5>
          <p>${products[i]['price']}</p>
        </div>`
    cardWrap.insertAdjacentHTML('beforeend', card);
});

 

์•Œ๊ฒŒ ๋œ ์ 

.append()์˜ ๊ฒฝ์šฐ jquery ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์—, cardWrap์„ querySelector ํ˜•ํƒœ๋กœ ์„ ์–ธํ•˜๋‹ˆ ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ๋œจ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. append๋ฅผ ์“ธ ๋•Œ ์†Œ์Šค ํ•จ์ˆ˜๋Š” jquery ํ˜•ํƒœ๋กœ ์“ฐ์—ฌ์ ธ์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค.
๋ฐ˜๋ฉด, .appendChild()๋Š” ์›์‹œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์ด๋‹ค. .append()์ฒ˜๋Ÿผ ์•ˆ์— ์š”์†Œ๋ฅผ ๊ทธ๋ƒฅ ์ง‘์–ด๋„ฃ์„ ์ˆ˜ ์—†๊ณ , creadteElement๋กœ ์š”์†Œ๋ฅผ ๋งŒ๋“  ํ›„ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.


6. ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ajax๋กœ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

์กฐ๊ฑด

๋”๋ณด๊ธฐ๋ฅผ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์ƒํ’ˆ์„ ๊ฐ€์ ธ์˜ค๊ณ  3๋ฒˆ์งธ ํด๋ฆญ ์‹œ ๋ฒ„ํŠผ์ด ์‚ฌ๋ผ์ ธ์•ผ ํ•œ๋‹ค.

let count = 0;
let cardWrap = document.querySelector('.row');
let button = document.querySelector('.btn-danger');

button.addEventListener('click', function(){
    count++;
    let insertData = function(data){
        data.forEach((a, i) => {
            let card =
            `<div class="col-sm-4">
            <img src="https://via.placeholder.com/600" class="w-100">
            <h5>${a.title}</h5>
            <p>${a.price}</p>
            </div>`
            cardWrap.insertAdjacentHTML('beforeend', card);
        });
    }
    if (count == 1){
        fetch('https://codingapple1.github.io/js/more1.json')
        .then(res => res.json())
        .then(data => {
            insertData(data);
        })
        .catch(error => {
            console.log(`์š”์ฒญ ์‹คํŒจ!`);
        })
    } else if (count == 2){
        fetch('https://codingapple1.github.io/js/more2.json')
        .then(res => res.json())
        .then(data => {
            insertData(data);
        })
        .catch(error => {
            console.log(`์š”์ฒญ ์‹คํŒจ!`);
        })
    } else if (count == 3){
        button.style.display = 'none';
    }
});

7. ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

const arr = ['cherry', 'date', 'apple', 'banana'];
arr.sort(function(a, b) {
  if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  } else {
    return 0;
  }
});
console.log(arr)

์ด๋Š” js์—์„œ ๋ฌธ์ž์—ด์ด ๋ถ€๋“ฑํ˜ธ๋กœ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ํ™œ์šฉํ•œ ์ฝ”๋“œ์ด๋‹ค. ์ด๋กค ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜๋Š” ์—ญ์ˆœ ์ •๋ ฌ์ด๋‹ค.

arr.sort(function(a, b) {
  if (a < b) {
    return 1;
  } else if (a > b) {
    return -1;
  } else {
    return 0;
  }
});

8. ๊ฐ€๊ฒฉ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

์กฐ๊ฑด

๊ฐ€๊ฒฉ์ˆœ ์ •๋ ฌ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์ƒํ’ˆ์„ ๋ชจ๋‘ ์‚ญ์ œ ํ›„ ๊ฐ€๊ฒฉ์ˆœ ์ •๋ ฌ๋œ ์ƒํ’ˆ์œผ๋กœ ๋ฟŒ๋ ค์ค€๋‹ค.

let products = [
    { id : 0, price : 70000, title : 'Blossom Dress' },
    { id : 1, price : 50000, title : 'Springfield Shirt' },
    { id : 2, price : 60000, title : 'Black Monastery' }
]; 
let cardWrap = document.querySelector('.row');
let buttonMore = document.querySelector('.btn-more');
let buttonSort = document.querySelector('.btn-sort');

buttonMore.addEventListener('click', function(){
    products.forEach(function(a, i){
        let card = `<div class="col-sm-4">
            <img src="https://via.placeholder.com/600" class="w-100">
            <h5>${products[i]['title']}</h5>
            <p>${products[i]['price']}</p>
            </div>`
        cardWrap.insertAdjacentHTML('beforeend', card);
    });
});
buttonSort.addEventListener('click', function(){
    cardWrap.innerHTML = '';
    products.sort(function(a, b) {
        let priceA = a.price;
        let priceB = b.price;
        if (priceA < priceB){
            return -1;
        } else if (priceA > priceB){
            return 1;
        } else {
            return 0;
        }
        // return a.price - b.price ํ•ด๋„ ๋จ;
    });
    products.forEach(function(a, i){
        let card = `<div class="col-sm-4">
            <img src="https://via.placeholder.com/600" class="w-100">
            <h5>${products[i]['title']}</h5>
            <p>${products[i]['price']}</p>
            </div>`
        cardWrap.insertAdjacentHTML('beforeend', card);
    });
});

์ถœ์ฒ˜ : ์ฝ”๋”ฉ์• ํ”Œ https://codingapple.com/