์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- next13 emotion
- emotion RSC
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- js ๋ฌธ์์ด ์์ถ
- ์ค์ฝํ
- suspense ๋ณ๋ชฉํ์
- app router emotion
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- ์นด์นด์ค ์ฝํ
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
- suspense react-query
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- js
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- ๋ฐฑ์ค 2108 nodejs
- ์ฌ์ฉ์ฑ ๊ฐ์
- ๊ตฌ๋ฆํค
- js ์ค์ฝํ
- ๋ฐฑ์ค 1339๋ฒ nodejs
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ์ต์ ๋์ฒด์ด๋
- suspense ๋์์๋ฆฌ
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๋ฐฑ์ค 1339๋ฒ js
- suspense ๋น๋๊ธฐ
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- emtion app router
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- Today
- Total
Lennon FE
[JavaScript] lodash sortBy ๋ณธ๋ฌธ
react, javascript์์๋ ๋ณดํต sort๋ฅผ ์ด์ฉํด ์ ๋ ฌ์ ๋ง์ดํ๋ค.
๊ทธ๋ฌ๋ sort๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋๊ฐ ์๋ค์ํผ ์ฝ๋ ๊ฐ๋
์ฑ์ด ๋ง์ด ๋จ์ด์ง ์ ๋ฐ์ ์๋ค.
๋ํ sort๋ ์ด์ํ ๋ฌธ์ ์ ์ด ์๋ค.
const arr = [1,3,20,5];
const sortedArr = arr.sort();
console.log(sortedArr) // [1,20,3,5]
sort๋ฅผ ๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ์์ฒ๋ผ ์ฐ๋ฆฌ๊ฐ ์์ํ๋๋ก ๋์ํ์ง ์๋๋ค...(์๋ฐ์คํฌ๋ฆฝํธ... ใ .ใ ๐ฅฒ)
๊ทธ๋ก์ธํด ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์๋ผ๋ฉด
๋ณดํต ๊ธฐ๋ณธ sort๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ์ง ์๊ณ (a,b) ๋ ์ธ์๋ฅผ ๋น๊ตํ๋ ํจ์๋ฅผ ๋ง๋ค์ด sort๋ฅผ ์ฌ์ฉํ๋ค.
const arr = [1,3,20,5];
const sortedArr = arr.sort((a,b) => a - b);
console.log(sortedArr) // [1,3,5,20]
๊ธฐ๋ณธ์ ์ธ ์์๋ฅผ ํตํด sort๋ฅผ ์ฌ์ฉํ ๋ ์ฝ๋์ ๊ฐ๋ ์ฑ์ด ์ด๋ค์ง ํ์ธํด๋ณด์!
const userData = [
{ id: 5, name: 'kenny', birth: 19970529},
{ id: 2, name: 'mile', birth: 19960122},
{ id: 2, name: 'toil', birth: 19920912},
{ id: 4, name: 'jack', birth: 20010621},
{ id: 7, name: 'hendy', birth: 20071121},
]
์์ ๊ฐ์ด ์๋ฒ์์ objectํํ๋ก ๋ ๋ผ์จ userData๊ฐ ์กด์ฌํ๋ค๊ณ ์๊ฐํด๋ณด์.
๋จผ์ id ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด๋ณด์!
function App() {
const userData = [
{ id: 5, name: "kenny", birth: 19970529 },
{ id: 2, name: "mile", birth: 19960122 },
{ id: 2, name: "amily", birth: 19920912 },
{ id: 4, name: "jack", birth: 20010621 },
{ id: 7, name: "hendy", birth: 20071121 },
];
const sortedUserId = userData.sort((a, b) => a.id - b.id);
console.log(sortedUserId);
return <></>;
}
์ฌ๊ธฐ๊น์ง ๊ด์ฐฎ์ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ฌ๋ id๊ฐ ๊ฐ์ ๋ name์ ์ํ๋ฒณ ์์๋ก ๋ค์ ์ ๋ ฌํ๊ณ ์ถ๋ค.
function App() {
const userData = [
{ id: 5, name: "kenny", birth: 19970529 },
{ id: 2, name: "mile", birth: 19960122 },
{ id: 2, name: "mile", birth: 19920912 },
{ id: 4, name: "jack", birth: 20010621 },
{ id: 7, name: "hendy", birth: 20071121 },
];
const sortedUserId = userData.sort((a, b) => {
if (a.id === b.id) {
if (a < b) return -1;
if (a > b) return 1;
if (a === b) return 0;
else return -1;
}
return a.id - b.id;
});
console.log(sortedUserId);
return <></>;
}
๋ญ๊ฐ ๋ ๋ณต์กํด์ก๋ค. ๊ทธ๋ฌ๋ ์ด๋ฆ๋ ๊ฐ๋ค๋ฉด ๋์ด๊ฐ ๋ง์ ์์๋๋ก ์ ๋ ฌํ๊ณ ์ถ์ด์ก๋ค.
function App() {
const userData = [
{ id: 5, name: "kenny", birth: 19970529 },
{ id: 2, name: "mile", birth: 19960122 },
{ id: 2, name: "mile", birth: 19920912 },
{ id: 4, name: "jack", birth: 20010621 },
{ id: 7, name: "hendy", birth: 20071121 },
];
const sortedUserId = userData.sort((a, b) => {
if (a.id === b.id) {
if (a < b) return -1;
if (a > b) return 1;
if (a === b) {
return a.birth - b.birth;
} else return -1;
}
return a.id - b.id;
});
console.log(sortedUserId);
return <></>;
}
๋ญ๊ฐ ์ฌ์ค ๊ทธ๋ฅ ์ ๋ ฌ๋ง ํ๊ณ ์ถ์์ ๋ฟ์ธ๋ฐ ์ฝ๋๊ฐ ๋ง์ด ๋์กํด์ก๋ค. ์ด๋ฅผ ๋ ๋ฉ์ง๊ฒ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด ์์๊น?
sortBy()
๊ทธ๋์ sortBy()๊ฐ ์กด์ฌํ๋ค!
lodash์ sortby๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
lodash ์ค์น ๋ฐฉ๋ฒ
$ npm i -g npm
$ npm i --save lodash
๋ฆฌ์กํธ๋ฅผ ์์๋ก ์ ๋ง์ง๋ง ์ฝ๋๋ฅผ sortBy๋ฅผ ์จ์ ํ์ํด๋ณด๋ฉด
import sortBy from "lodash/sortBy";
function App() {
const userData = [
{ id: 5, name: "kenny", birth: 19970529 },
{ id: 2, name: "mile", birth: 19960122 },
{ id: 2, name: "mile", birth: 19920912 },
{ id: 4, name: "jack", birth: 20010621 },
{ id: 7, name: "hendy", birth: 20071121 },
];
const sortedUserId = sortBy(userData, "id", "name", "birth");
console.log(sortedUserId);
return <></>;
}
export default App;
์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ์ค์ ํ ์ ์๋ค...!
reverse()๋ฅผ ์จ์ ์ญ์ ์ ๋ ฌ๋ ๊ฐ๋ฅํ๋ฉฐ, ์ํ๋ ๋๋ก ์ด์ด๊ฐ๋ฉฐ ์ ๋ ฌ์ ํ ์ ์๋ค.
import sortBy from "lodash/sortBy";
function App() {
const userData = [
{ id: 5, name: "kenny", birth: 19970529 },
{ id: 2, name: "mile", birth: 19960122 },
{ id: 2, name: "mile", birth: 19920912 },
{ id: 4, name: "jack", birth: 20010621 },
{ id: 7, name: "hendy", birth: 20071121 },
];
const sortedUserId = sortBy(userData, "id", "name", "birth").reverse();
console.log(sortedUserId);
return <></>;
}
export default App;
sortBy๋ฅผ ์๋ ๊ฐ๋ฐ์๋ผ๋ฉด ์ ์ฝ๋๊ฐ ๋ ๋งค๋ ฅ์ ์ธ ์ฝ๋๋ก ๋ณด์ผ ์ ์์ ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ๋ค.
ํ๋ก ํธ๋จ์์ object ํ์์ ์๋ฒ ๋ฐ์ดํฐ๋ฅผ props๋ก ๋ฐ์ ๋์ธ ๋ sortBy๋ฅผ ์ฌ์ฉํด ๊ฐ๋จํ๊ฒ ๊ตฌํํ ์ ์์ ๊ฒ ๊ฐ๋ค.
'๐งโ๐ป Web > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Array, Set, Map์ ํตํด ์์๋ณด๋ ์ดํฐ๋ฌ๋ธ/์ดํฐ๋ ์ดํฐ (0) | 2022.07.19 |
---|---|
[JavaScript] ๊ณ ์ฐจํจ์ (0) | 2022.07.18 |
[JavaScript] ํ์ดํ ํจ์์ ์ผ๋ฐ ํจ์์ ์ฐจ์ด (0) | 2022.06.03 |
[heroku] nodejs ๋ฐฐํฌํ ๋ dotenv ์ฒ๋ฆฌ (0) | 2022.03.25 |
billboard.js ์ ๋ํด ์์๋ณด์. (pie chart ์์) (0) | 2022.03.24 |