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/
'๐จ JavaScript > ๋ฌธ์ ํ๊ธฐ (ํ๋ก๊ทธ๋๋จธ์ค, ์ฝ๋ฉ์ ํ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Level 1] ์ผ์ด์ฌ (0) | 2023.12.10 |
---|---|
Level 3 : 9 ์ฅ๋ฐ๊ตฌ๋๋ฒํผ ๊ตฌํํ๊ธฐ(localStorage, array, forEach, JSON) (0) | 2023.04.15 |
Level 3 : 4 (array, object, forEach) (0) | 2023.01.31 |
Level 3 : 1~3 (array, object) (0) | 2023.01.31 |
[์ค์] Level 2 : 14,15(์์ ๋ฐ ๊นจ๋ฌ์ ์ )_for, ๋ฒ๋ธ๋งํ์ฉ์ถ์ฝ๋ฒ์ (0) | 2023.01.13 |