[JavaScript30] 7. Array Cardio Practice (2) | .some(), .every(), .find() and [...SPREADS]
language/javascript

[JavaScript30] 7. Array Cardio Practice (2) | .some(), .every(), .find() and [...SPREADS]

๐Ÿ“ƒ ์š”๊ตฌ์‚ฌํ•ญ

์ฃผ์–ด์ง„ 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