Untitled
// const finalList = myFilter.reduce ((a,c) => { // if(c.category !== a.lastCategory){ // a.output = [...a.output,<ProductCategoryRow key={c.category} category={c.category}/>] // a.lastCategory = c.category // } // a.output = [...a.output,<ProductRow key={c.name} product={c}/>] // return a // },{lastCategory:null,output: []}) // console.log(finalList) //ฉบับสายย่อ const finalList = myFilter.reduce((a, c) => { if (c.category !== a.lastCategory) a.output.push(<ProductCategoryRow key={c.category} category={c.category} />); a.output.push(<ProductRow key={c.name} product={c} />); a.lastCategory = c.category; return a; }, { lastCategory: null, output: [] }); console.log(finalList);
Leave a Comment