211203 ๊ฐœ๋ฐœ๊ธฐ๋ก: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ์กฐ๊ฑด๋ณ„ ์ •๋ ฌ, ์นดํ…Œ๊ณ ๋ฆฌ ์ •๋ ฌ, ์ด์ค‘ ์ •๋ ฌ

2021. 12. 3. 22:02ใ†๐Ÿ“” TIL

[ ๋ฌธ์ œ ]

์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค. status  ํ”„๋กœํผํ‹ฐ์—๋Š” "๋Œ€๊ธฐ", "์ง„ํ–‰์ค‘", "์™„๋ฃŒ" 3๊ฐ€์ง€ ๊ฐ’์ด ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๊ณ , ์š”๊ตฌ์‚ฌํ•ญ์€ ๋ฐฐ์—ด ๋‚ด๋ถ€์˜ ๊ฐ์ฒด๋ฅผ "๋Œ€๊ธฐ - ์ง„ํ–‰์ค‘ - ์™„๋ฃŒ" ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

const patients = [
    {
        "id": 1,
        "name": "๊น€๋”์กด",
        "reg_time": "14:30",
        "doc_name": "๊น€์‹œ์—ฐ",
        "status": "์™„๋ฃŒ",
    },
    {
        "id": 2,
        "name": "๋ฐ•๋”์กด",
        "reg_time": "12:30",
        "doc_name": "์ตœ์ง„์ˆ˜",
        "status": "๋Œ€๊ธฐ",
    },
    {
        "id": 3,
        "name": "๋Œ€๋”์กด",
        "reg_time": "11:30",
        "doc_name": "์ด์ƒ์šฑ",
        "status": "์ง„ํ–‰์ค‘",
    },
  ...,
      {
        "id": 10,
        "name": "์ตœ๋”์กด",
        "reg_time": "11:30",
        "doc_name": "์ž„์„ฑํ›„",
        "status": "์ง„ํ–‰์ค‘",
    },
]

 

ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ Œ๋”๋งํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜จ๋‹ค. ๋’ค์ฃฝ๋ฐ•์ฃฝ์ด๋‹ค. ์ • ๋ ฌ ํ•˜๋Ÿฌ ๊ฐ€๋ณด์ž๊ณ 

 

 

 

 

[ํ•ด๊ฒฐ๊ณผ์ •]

sort()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ์ง๊ด€์ ์ด์ง€ ์•Š์•˜๋‹ค. (์‚ฌ์‹ค sort() ๋ฉ”์†Œ๋“œ๋กœ ํ•ด๊ฒฐ๋ชปํ–ˆ๋‹ค, ํ•ด๊ฒฐ๋„ ๋ชปํ•˜๊ณ  ์ง๊ด€์ ์ด์ง€๋„ ์•Š๊ณ )

์šฐ์„  ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์ˆœ์„œ๋Œ€๋กœ(๋Œ€๊ธฐ -> ์ง„ํ–‰์ค‘ -> ์™„๋ฃŒ)  order ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

const order = ["๋Œ€๊ธฐ", "์ง„ํ–‰์ค‘", "์™„๋ฃŒ"]

 

order ์˜ ์›์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ iterate ํ•˜๋ฉด์„œ, ์šฐ๋ฆฌ๊ฐ€ ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” patients ๋ฐฐ์—ด์— filter ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ–ˆ๋‹ค.

 

const patientOrder = order.map(base => 
                          patients.filter(data => data.status === base));

 

์ด๋Ÿฐ์‹์œผ๋กœ ์„ธ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ patientOrder ๋ฐฐ์—ด์— ๋‹ด๊ฒผ๋‹ค.

0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€, ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ˆœ์„œ์ธ ๋Œ€๊ธฐ -> ์ง„ํ–‰์ค‘ -> ์™„๋ฃŒ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์ด ์ž˜ ์ €์žฅ๋ผ์žˆ๋‹ค.

ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ์„œ๋กœ ๋‹ค๋ฅธ ์„ธ๊ฐœ์˜ ๋ฐฐ์—ด์ด ๋‹ด๊ฒจ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํ˜•ํƒœ๋Š” ๊ทธ์ € ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค. (๊ทธ๋ž˜์•ผ ๋ Œ๋”๋ง์ด ๋˜๋‹ˆ๊นŒ)

ํ•ด๊ฒฐ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. flat() ๋ฉ”์„œ๋“œ๋ฅผ ์จ์ฃผ์ž. 

 

const patientOrder = order.map(base => 
                          patients.filter(data => data.status === base)).flat();

 

 

[ ๊ฒฐ๊ณผ ]

๊น”~๋”ํ•˜๊ฒŒ ์ •๋ ฌ ๋˜์—ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ์ง€๋งŒ ๊ธฐ๋กํ•˜๊ณ  ์‹ถ์–ด์„œ ํฌ์Šคํƒฑํ–ˆ๋‹ค.