const object = {'a': 1, 'b': 2, 'c' : 3};
for(let x in object) {
console.log(x)
}
// result: 'a' 'b' 'c'
const object = {'a': 1, 'b': 2, 'c' : 3};
Object.keys(object)
// ['a' 'b' 'c']
const object = {'a': 1, 'b': 2, 'c' : 3};
// Object.values(object): [ 1, 2, 3 ]
for(let x of Object.values(object)) {
console.log(x)
}
// result: 1 2 3
const object = {'a': 1, 'b': 2, 'c' : 3};
// Object.entries(object): [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
for (const [key, value] of Object.entries(object)) {
console.log(key, value);
}
// result: 'a' 1 'b' 2 'c' 3
이차원 배열이 정사각형이 아닌 직사각형인 경우 고려하여 작성하였음
const arr = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]];
const result = [];
for(let i=0; i<=arr.length-1; i++) {
for (let j = 0; j<=arr[i].length-1; j++) {
result.push(arr[i][j])
}
}
// result: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
// ➡ ⬅ 순으로 지그재그로 출력하기
for (let i = 0; i<=arr.length-1; i++) {
for (let j = 0; j<=arr[i].length-1; j++) {
if (i % 2) {
result.push(arr[i][arr[i].length-1-j])
} else {
result.push(arr[i][j])
}
}
}
// result: [ 1, 2, 3, 6, 5, 4, 7, 8, 9, 12, 11, 10 ]
const arr = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]];
const result = [];
for (let j = 0; j<=arr[0].length-1; j++) {
for(let i=0; i<=arr.length-1; i++) {
result.push(arr[i][j])
}
}
// result: [ 1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12 ]
const arr = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]];
const result = [];
for (let j = arr[0].length-1; j>= 0; j--) {
for(let i=arr.length-1; i>=0; i--) {
result.push(arr[i][j])
}
}
// result: [ 12, 9, 6, 3, 11, 8, 5, 2, 10, 7, 4, 1 ]