오늘 알게 된 거 5 (Array Method)

hojoon·2023년 8월 16일
0

자바스크립트

목록 보기
12/14

생각해보니 오늘 알게 된 거, 지식 이거 멋있는 말로 TIL이라고 있었음 ㅋㅋㅋ

Javascript에는 map, filter만 있는게 아니다.

회사에 들어오고 나서 코드를 파악하는데 some, includes, find 등등 생소했던 배열 method들이 있었다. 물론 map, filter, reduce와 같은 것들을 알고 활용할줄 안다면 이해하는데 어렵지 않겠지만 알고 있다면 정말 유용할것 같고 아직도 공부할게 산더미구나 라고 생각했었다.! 그래서 이참에 모르는 method들을 정리해서 기록해보기로 함 ㅋㅋ!

every

  • 배열의 모든 요소가 제공한 함수로 구현된 테스트를 통과하는지를 테스트

some

  • some() 메서드는 배열 안의 어떤 요소라도 주어진 판별 함수를 적어도 하나라도 통과하는지 테스트합니다. 만약 배열에서 주어진 함수가 true을 반환하면 true를 반환합니다. 그렇지 않으면 false를 반환합니다. 이 메서드는 배열을 변경하지 않습니다.

reverse

  • 배열의 원소 순서를 거꾸로 바꾼다.

at

  • at() 메서드는 정수 값을 받아, 배열에서 해당 값에 해당하는 인덱스의 요소를 반환합니다. 양수와 음수 모두 지정할 수 있고, 음수 값의 경우 배열의 뒤에서부터 인덱스를 셉니다.
const array1 = [5, 12, 8, 130, 44];

let index = 2;

console.log(`Using an index of ${index} the item returned is ${array1.at(index)}`);
// Expected output: "Using an index of 2 the item returned is 8"

index = -2;

console.log(`Using an index of ${index} item returned is ${array1.at(index)}`);
// Expected output: "Using an index of -2 item returned is 130"

copyWithin()

  • arr.copyWithin(target[, start[, end]])
  • copyWithin() 메서드는 배열의 일부를 얕게 복사한 뒤, 동일한 배열의 다른 위치에 덮어쓰고 그 배열을 반환합니다. 이 때, 크기(배열의 길이)를 수정하지 않고 반환합니다.
onst array1 = ['a', 'b', 'c', 'd', 'e'];

// Copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4));
// Expected output: Array ["d", "b", "c", "d", "e"]

// Copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3));
// Expected output: Array ["d", "d", "e", "d", "e"]

reduceRight

  • reduceRight() 메서드는 누적기에 대해 함수를 적용하고 배열의 각 값 (오른쪽에서 왼쪽으로)은 값을 단일 값으로 줄여야합니다.

무슨말이냐??

코드로 보는게 젤 이해가 빠르다.

const array1 = [
  [0, 1],
  [2, 3],
  [4, 5],
];

const result = array1.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue));

console.log(result);
// Expected output: Array [4, 5, 2, 3, 0, 1]

간단하게 작성하려고 했는데 MDN에서 ArrayMethod를 찾아보니까 진짜 뭐가 엄청 많다 열심히 공부해야한다는걸 또 느꼈다 ㅋㅋㅋ

profile
프론트? 백? 초보 개발자의 기록 공간

0개의 댓글