• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 'array에 자주 쓰는 sort, map, filter 함수' 숙제

'array에 자주 쓰는 sort, map, filter 함수' 숙제

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #90796

    임동희
    참가자
    var productList2 = [
        { id: 0, price: 70000, title: 'Blossom Dress' },
        { id: 1, price: 50000, title: 'Springfield Shirt' },
        { id: 2, price: 60000, title: 'Black Monastery' }
    ];
    $('.row').html('')
    productList2.forEach((product) => {
        var template = `<div class="col-sm-4">
        < img src="https://via.placeholder.com/600" class="w-100">
        <h5>${product.price}</h5>
        <p>가격 : ${product.title}</p>
    </div>`
        $('.row').append(template)
    })
    sortObject = {
        priceSortbtn:function(a, b){return a.price - b.price},
        nameSortBtn: function(a, b) {
            if (a.title > b.title) return -1;
            return 1;
        },
        filterBtn: (a) => a.price < 60000,
        aa:5,
    }
    $('.btn').click(function () {
        var temp = productList2
        if (this.id == 'filterBtn') {
            temp = temp.filter(sortObject[this.id])
        }
        if (this.id == 'priceSortBtn') {
            temp.sort(sortObject[this.id])
            console.log(sortObject[this.id])
        }
        if (this.id == 'nameSortBtn') {
            temp.sort(sortObject[this.id])
        }
        // productList2.sort((a,b)=>{
        //     return a.price - b.price
        // })
        console.log(temp, sortObject[this.id])
        $('.row').html('')
        temp.forEach((product) => {
            var template = `<div class="col-sm-4">
        < img src="https://via.placeholder.com/600" class="w-100">
        <h5>${product.price}</h5>
        <p>가격 : ${product.title}</p>
    </div>`
            $('.row').append(template)
        })
    })
    
    sort 관련 함수들이 value로 들어있는 경우 sortObject[this.id] 가 undefined로 뜨면서 정렬작업이 안되는데 이유가 뭘까요?
    
    filter는 동작이 됩니다.
    #90839

    codingapple
    키 마스터
    id 출력할 때 .id말고 .attr('id') 이런거 써봅시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠