프로그래밍 정보공유

중복 제거된 object array parsing하기

student513 2020. 7. 13. 11:06

변경전

const uniqueFlatQna = [].concat.apply([], faqs.map((faq)=>{
            return faq.faqs.map((qa)=>{
                return {
                    question: qa.question,
                    answer: qa.answer,
                    fUrl: qa.fUrl,
                }
            })
        }))
        uniqueFlatQna.forEach((item) => {
            if (uniqueFlatQna.findIndex(x => x.question === item.question) <= -1) 
                uniqueFlatQna.push({item})
        })
        
        const data = this.state.keyword ? uniqueFlatQna.filter(
            (qna) => {
                return qna.question.indexOf(this.state.keyword) > -1;
            }
        ) : faqs[selectedCategoryIndex]?.faqs || []

변경 후

getKeywordFaqs(faqData, keyword) {
        const keywordFaqs = []
        faqData.forEach((section) => {
            section.faqs.forEach((qna) => {
                if (qna.question.indexOf(keyword) >= 0 && keywordFaqs.includes(qna) === false) keywordFaqs.push(qna)
            })
        })
        return keywordFaqs
}

const data = keyword ? this._getKeywordFaqs(faqs, keyword) : (faqs[selectedCategoryIndex]?.faqs || [])