코딩물고기의 IT월드
[JS] 반복문, for, each
코딩하는물고기
2020. 8. 11. 23:55
728x90
let text = 'hello'
- 작은 따옴표 권장
- ; 있어도 되고, 없어도됨
let = true, false : boolean
null- 진짜 없다 , undefined- 아직 발견되지 않았다. - 없다
------------------
연산자
대입 연산자 = let a =1 ;
산술 연산자
- 사칙연산
a += 1
a -= 3
논리연산자
not !
and &&
or ||
----------------------------- for
const numbers = [10, 20, 30, 40, 50];
const doggy = {
name: "멍멍",
sound: "왈왈",
age: 2
};
console.log(Object.entries(doggy));
console.log(Object.keys(doggy));
console.log(Object.values(doggy));
----------------------------- for in
const doggy = {
name: "멍멍",
sound: "왈왈",
age: 2
};
for (let key in doggy){
console.log(`${key}: ${doggy[key]}`)
}
--------------------------- break
for(let i =0; i <10; i++){
if(i===2) continue;
console.log(i);
if(i===5) break;
}
----------------------------- let
function sumOf(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
const result = sumOf([1, 2, 3, 4, 5]);
console.log(result);
------------------------------ for
function biggerThanThree(numbers) {
const array = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 3) {
array.push(numbers[i]);
}
}
return array;
}
const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // [4, 5, 6, 7]
export default biggerThanThree;
------------------------------ 배열
const superHeroes = ["아이언맨", "캡틴아메리카"];
for(let i =0; i {
squard.push(n * n);
})
console.log(squard);
---
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const squared = array.map(n => n * n);
console.log(squared);
-- map 예제
const items = [
{
id: 2,
text: "hello"
},
{
id: 3,
text: "bye"
}
];
const texts = items.map((item) => item.text);
console.log(texts);
----
indexOf = 배열 내장함수
const superheroes = ["아이언맨", "캡틴", "토르"];
const index = superheroes.indexOf("토르");
console.log(index);
------------------ findIndex
const todos = [
{
id: 1,
text: "자바스크립트 입문",
done: true
},
{
id: 2,
text: "함수배우기",
done: true
},
{
id: 3,
text: "객체와 배열 배우기",
done: true
},
{
id: 4,
text: "배열 내장함수 배우기",
done: false
}
];
const index = todos.findIndex(todo => todo.id ===3);
console.log(index);
--- 글씨도 찾고 싶을 떄 find 사용
const todos = [
{
id: 1,
text: "자바스크립트 입문",
done: true
},
{
id: 2,
text: "함수배우기",
done: true
},
{
id: 3,
text: "객체와 배열 배우기",
done: true
},
{
id: 4,
text: "배열 내장함수 배우기",
done: false
}
];
const todo = todos.find((todo) => todo.id === 3);
console.log(todo);
728x90