티스토리 뷰
ES6-for of 순회하기
>es6 에 새로 나온 배열을 순회하는 for of에 대하여 알아보자.
//1.foreach
var data = [1,2,undefined,NaN,null,""]
//foreach로도 순회하여 값을 알 수 있음.
data.forEach(function(value){
console.log(value);
})
//2.for in
//for in은 문제점이 자기자신이 갖고있지 않은 상위에 있는 추가된 값 까지도 표현하여 나타남
for(let idx in data){
console.log(data[idx])
}
//로 찍어 주면 function(){}도 콘솔에 찍힘. ->잘못됨
ex) Array.prototype.getIndex =function(){};
라고 추가해주면,
for(let idx in data){
console.log(data[idx])
}
//3.for of
//for in에 대한 실수를 줄이기 위해 for of를 통해 배열을 순회할 수 있음.
for(let value of data){
console.log(data[value]);
}
For of로 문자열도 순회할 수 있다.
var str="hello world!!!!";
for(let value of str){
console.log(value);
}
'Programming Skills > es6' 카테고리의 다른 글
ES6-from 메서드로 진짜 배열 만들기 (0) | 2018.02.20 |
---|---|
ES6-Spread operator (0) | 2018.02.20 |
ES6- ES2015 string에 새로운 메서드들 (0) | 2018.02.16 |
es6-let과 closure, const (0) | 2018.02.10 |
ES6-es2015 시작하기,let (0) | 2018.02.09 |