rows.flat() 2차원배열-> 1차원 배열, every(), some()

Web Development assistant·2022년 2월 6일
0

# javascript

목록 보기
12/36
post-thumbnail

const rows = [];
    
for(let i = 0 ; i < 3; i++){
	const row =[];
	for(let j = 0; j < 3; j++){
    	const cell = [];
    	row.push(cell);
    }
    rows.push(row);
}

console.log(rows);			

//모두...
rows.flat().every((td)=>td.textContent);		//td에 값이 전부 존재할 경우 true

//하나라도...
rows.flat().some((td)=>td.textContent);

2차원 배열을 1차원으로 바꿔준다.

every() = 모두 조건이 참이라면 ? true : false;
some() = 하나라도 조건이 참이라면 ? true : false;

이때
rows.flat().some((td)=>td.textContent);
td.textContent는 리턴 값을 의미하는데

리턴 값이 false가 되는 7가지 경우
1.문자열의 빈 문자열
2.boolean false
3.숫자 0
4.null
5.undefined
6.NaN
7.document.array

0개의 댓글