Array.prototype.~

JIWON LEE·2021년 3월 22일

javascript array 함수 간단히 정리


  1. Array.prototype.concat()
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
  • 배열을 합쳐서 새로운 배열 반환
  1. Array.prototype.fill()
const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]
  • 배열의 시작 인덱스부터 끝 인덱스 이전까지 정적인 값으로 채움
  1. Array.prototype.includes()
const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false
  • 배열이 특정 요소를 포함하는지 판별
profile
포기잘하는 프론트엔드 개발자

0개의 댓글