include, join, reverse

개발 및 관리/Javascript 2023. 11. 18. 18:51 posted by HighLighter
반응형


//[16]: 배열에 특정 요소가 포함되어 있는지 없는지 알기 --> include() 메서드

const testAr16 = ['Korea','Usa','China','Japan','Canada'];
console.log(testAr16);

console.log(testAr16.includes('Korea')); //true
console.log(testAr16.includes('Russia')); //false

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//[17] : 배열내 요소들을 하나로 묶어주기 --> 그때, 문자열(String) 타입으로 묶어주기 --> join()
const testAr17 = ['Korea','Usa','China','Japan','Canada'];
console.log(testAr17);

console.log(testAr17.join()); // 아무 옵션도 넣지 않고 호출하면 배열 요소 각각을 콤마(,)로 공백도 없이 묶어줌.
console.log('결과 타입은= ' + typeof testAr17.join()); //string

console.log(testAr17.join('-'));  // 넣어주는 구분자로 각각의 요소를 구분하여 하나의 문자열로 이어줌
console.log(testAr17.join(' '));
console.log(testAr17.join(', '));
console.log(testAr17.join('+'));
console.log(testAr17.join('/'));

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//요소가 undefined, null이 있을 때 join() 메서드를 사용하면 ? --> 빈 문자열로 연결
const testAr17_temp1 = ['Korea', 'Usa', undefined, 'China', 'Japan', null, 'Canada'];
console.log(testAr17_temp1);
console.log(testAr17_temp1.join(', '));

//빈 배열이면? --> 빈 문자열로 반환
const testAr17_temp2 = [];
console.log(testAr17_temp2);
console.log(testAr17_temp2.join(', ')); // 구분자를 넣어봤자 상관없이 결과는 --> ""
console.log(typeof [].join(', ')); // string

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//[18] : 배열내 요소들을 거꾸로 출력하기 --> reverse();
const testAr18  = ['a', 'b', 'c', 'd', 'e'];
console.log(testAr18);
console.log(testAr18.reverse()); // e, d, c, b, a
console.log(testAr18); // 원본 배열을 가지고 작업처리를 하기 때문에 --> 거꾸로 되어져 있음.

반응형

'개발 및 관리 > Javascript' 카테고리의 다른 글

isNan() vs Number.isNaN() 비교  (0) 2023.11.18
find, filter  (0) 2023.11.18
숫자, 문자 찾기  (0) 2023.11.18
인덱스 몇 번째에 있는지 찾기 indexOf  (1) 2023.10.31
배열 붙이기 concat() 메서드  (0) 2023.10.31