๐ ์๊ตฌ์ฌํญ
์ฃผ์ด์ง data๋ค์ ๊ฐ์ง๊ณ , ์กฐ๊ฑด์ ๋ง๋ ๊ฒฐ๊ด๊ฐ์ console ์ฐฝ์ ๋์ด๋ค.
๐ป ์์ค์ฝ๋
๋จผ์ , ์ฃผ์ด์ง data๋ ๋ค์๊ณผ ๊ฐ๋ค.
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
]
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 }
];
์ฌ๋๋ค์ ์ด๋ฆ, ์ถ์๋ ๋ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐฐ์ด๊ณผ, ๋๊ธ๋ค์ ๋ด์ฉ, id ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐฐ์ด์ด ์ฃผ์ด์ก๋ค.
๐ ๋ด๊ฐ ์์ฑํ ์ฝ๋
// Some and Every Checks
// Array.prototype.some() // is at least one person 19 or older?
const age_some = people.some((p) => {
return (new Date().getFullYear() - p.year + 1) >= 19
});
console.log(age_some)
// Array.prototype.every() // is everyone 19 or older?
const age_every = people.every((p) => {
return (new Date().getFullYear() - p.year + 1) >= 19
});
console.log(age_every)
// Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// find the comment with the ID of 823423
const comments_find = comments.find(i => {
return i.id === 823423
});
console.log(comments_find)
// Array.prototype.findIndex()
// Find the comment with this ID
const comments_findIndex = comments.findIndex(i => {
return i.id === 823423
});
console.log(comments_findIndex);
// delete the comment with the ID of 823423
comments.splice(comments_findIndex, 1);
console.log(comments);
๐ค ๊ฐ์ ๋ ๊ฐ์ ์ฝ๋
// Some and Every Checks
// Array.prototype.some() // is at least one person 19 or older?
const isAdult = people.some(person => (new Date()).getFullYear() - person.year >= 19);
console.log({isAdult});
// Array.prototype.every() // is everyone 19 or older?
const allAdults = people.every(person => (new Date()).getFullYear() - person.year >= 19);
console.log({allAdults});
// Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// find the comment with the ID of 823423
const comment = comments.find(comment => comment.id === 823423);
console.log(comment);
// Array.prototype.findIndex()
// Find the comment with this ID
// delete the comment with the ID of 823423
const index = comments.findIndex(comment => comment.id === 823423);
// comments.splice(index, 1);
const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1)
];
console.table(newComments);
console.table(comments);
๐ TIL
some()
๋ฐฐ์ด ์์ ์๋ ๊ฐ๋ค์ ์ํํ๋ฉด์, ํ๋์ ์์๋ผ๋ ์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ํต๊ณผํ๋ฉด ์ํ๋ฅผ ๋ฉ์ถ๊ณ true(boolean)๋ฅผ ๋ฆฌํดํ๋ค. ๋น ๋ฐฐ์ด์์ ํธ์ถํ๋ฉด ๋ฌด์กฐ๊ฑด false๋ฅผ ๋ฐํํ๋ค.
const array = [1, 2, 3, 4, 5];
const even = array.some((element) => element % 2 === 0);
console.log(even); // true
every()
๋ฐฐ์ด ์์ ์๋ ๊ฐ๋ค์ ์ํํ๋ฉด์, ๋ชจ๋ ์์๊ฐ ์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ํต๊ณผํ๋ฉด ์ํ๋ฅผ ๋ฉ์ถ๊ณ true(boolean)๋ฅผ ๋ฆฌํดํ๋ค.
array = [1, 2, 3, 4, 5];
const isBelowThreshold = array.every((currentValue) => currentValue < 40);
console.log(isBelowThreshold); // true
find()
์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์์ ๊ฐ์ ๋ฐํํ๋ค. ๋ง์กฑํ๋ ์์๊ฐ ์๋ค๋ฉด undefined๋ฅผ ๋ฐํํ๋ค.
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const found = array.find(element => element > 5);
console.log(found); // 6
findIndex()
์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์์ ๋ํ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค. ๋ง์กฑํ๋ ์์๊ฐ ์์ผ๋ฉด -1์ ๋ฐํํ๋ค.
const array = [5, 12, 8, 130, 44];
const isLargeNumber = array.findIndex((element) => element > 13);
console.log(isLargeNumber); // 3
Spread Operator
์ด์ ์ค์ต์์๋ Spread Operator๋ฅผ ์ฌ์ฉํด์ ํ ๋ฒ ์ ๋ฆฌํด ๋์๋๋ฐ, ์ด๋ฒ ์ค์ต์์๋ ์ฌ์ฉํ๊ฒ ๋์ด ๋ณต์ตํ ๊ฒธ ๋ค์ํ ๋ฒ ๊ฐ๋จํ ์์ฑํด๋ณด๋ ค๊ณ ํ๋ค.
Spread ์ฐ์ฐ์๋ ๊ธฐ์กด์ Object๋ฅผ ๊ฑด๋๋ฆฌ์ง ์์ผ๋ฉด์, ๊ทธ๊ฒ์ element๋ฅผ ํ๋์ฉ ๋ถ๋ฆฌํ์ฌ ์ ๊ฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
์ด๋ฒ ์ค์ต์ ๋ง์ง๋ง ๋ฌธ์ ๊ฐ ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ์์๋ฅผ ๊ธฐ์กด ๋ฐฐ์ด์์ ์ญ์ ํ๋ ๊ฒ์ธ๋ฐ, ๋ด๊ฐ ์ฌ์ฉํ ๋ฐฉ๋ฒ๋๋ก ํ๊ฒ ๋๋ฉด ๊ธฐ์กด์ ๋ฐฐ์ด ์์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ค.
comments.splice(comments_findIndex, 1);
๊ทธ๋์ ๊ฐ์์์๋ Spread Operator๋ฅผ ์ด์ฉํด์ ๊ธฐ์กด์ ๋ฐฐ์ด์ ๋ณํํ์ง ์๋, ์์๋ฅผ ๊ธฐ์กด ๋ฐฐ์ด์์ ์ญ์ ํ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๊ธฐ์กด์ comments ๋ฐฐ์ด์ด ๊ทธ๋๋ก ์กด์ฌํ๊ณ , ์์๊ฐ ์ญ์ ๋ ์๋ก์ด newComments ๋ฐฐ์ด๋ ์กด์ฌํ๊ฒ ๋๋ค.
const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1)
];
Redux์ ํต์ฌ ๊ท์น ์ค ํ๋๊ฐ ์ ๋ state๋ฅผ ์ง์ ๋ณ๊ฒฝํ์ง ์๊ณ ํ์กดํ๋ state๋ฅผ ๋ณด์กดํ๋ ๊ฒ์ด๋ผ๊ณ ํ๋ค. ๊ทธ๋์ ์๋ก์ด ๊ฐ์ ์ถ๊ฐํ๊ฑฐ๋ ์ ๋ฐ์ดํธํ ๋์๋ ๊ธฐ์กด์ ๊ฐ์ฒด๋ ์์ ํ์ง ์๊ณ , ๊ธฐ์กด ๊ฐ์ฒด์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด์ ๊ต์ฒดํ๋ ๋ฐฉ์์ผ๋ก ์ ๋ฐ์ดํธ๋ฅผ ํ๋ค.
๋ฐ๋ผ์ ์ด์ ๋ ์ด๋ค ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ์์ ์ ํ ๋, ๊ธฐ์กด ๊ฐ์ฒด์ ๋ณต์ฌ๋ณธ์ ์๋ก ์์ฑํด์, ๋ณต์ฌ๋ณธ์์ ํน์ ์์ ์ ์ํํ๋ ๋ฐฉ์์ ์ฌ์ฉํด์ผ ํ ๊ฒ ๊ฐ๋ค.
๐ ์ฐธ๊ณ
youtube Wes Bos
https://www.youtube.com/watch?v=QNmRfyNg1lw&list=PLu8EoSxDXHP6CGK4YVJhL_VWetA865GOH&index=7
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/some
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/every
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/find
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
'language > javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript30] 6. AJAX Type Ahead with fetch() | fetch(), Spread Operator, RegExp (0) | 2021.12.20 |
---|---|
[JavaScript30] 5. Flexbox + JavaScript Image Gallery (0) | 2021.12.10 |
[JavaScript30] 4. Array Cardio Practice (1) (0) | 2021.12.09 |
[JavaScript30] 3. CSS Variables | CSS ๋ณ์, setProperty() (0) | 2021.12.06 |
[JavaScript30] 2. Clock | setInterval(), transform, transition (0) | 2021.12.04 |