티스토리 뷰

Object VS Array 

- typeof 연산자를 배열에 사용하였을 때 둘다 "Object"로 출력 

 

(1) Object

- Index, length Property rk djqtdma 

- 리터럴 표기법 : {} 

const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}

- Object 값이 있는지 여부

let example = {};
Object.keys(example).length > 0 // false 

 

 

(2) Array 

const cars = ["Saab", "Volvo", "BMW"];

- 리터럴 표기법 : []

 

 

 

 

for 반복문

let cars = [
  {
    "color": "purple",
    "type": "minivan",
    "registration": new Date('2017-01-03'),
    "capacity": 7
  },
  {
    "color": "red",
    "type": "station wagon",
    "registration": new Date('2018-03-03'),
    "capacity": 5
  }]

 

 

for(let car of cars){ 
	console.log(car)
}
//{color: "purple", type: "minivan", registration: Tue Jan 03 2017 09:00:00 GMT+0900 (대한민국 표준시), capacity: 7}
// {color: "red", type: "station wagon", registration: Sat Mar 03 2018 09:00:00 GMT+0900 (대한민국 표준시), capacity: 5}

 

forEach 

(1)  Array 단순 사용 

const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));

(2) Object 사용 

ex)  array in Object 

 

 

item -> 각 줄

index -> 각줄에 해당하는 index 

array -> 전체 array 

cars.forEach(function(item,index,array){
   console.log(item) 
});
//{color: "purple", type: "minivan", registration: Tue Jan 03 2017 09:00:00 GMT+0900 (대한민국 표준시), capacity: 7}
//{color: "red", type: "station wagon", registration: Sat Mar 03 2018 09:00:00 GMT+0900 (대한민국 표준시), capacity: 5}

 

기존 object 에다가 각각 요소 추가

- return array를 추가 

cars.forEach(function(item,index,array){
   item["index"] = index + 1; 
   return array
})

 

Map 

- 반복문을 돌며 배열안의 요소들을 1대 1로 짝지어주기 

let number = [1,2,3,4]
let result = number.map(Math.sqrt) //  [1, 1.4142135623730951, 1.7320508075688772, 2]

 

 

reduce

- The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. 

 

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue; // 10 

 

 

include

 

 

 

 

 

 

filter 사용 

- 각 행을 filter하여 속성 color가 "red"인 경우 찾기 

let filteredData = cars.filter((data) => {
   return data.color === "red"
});

 

 

 

대문자, 소문자로 바꾸기

Ref) Document

let name = "rosy"
name.toUpperCase() //ROSY

let name = "ROSY"
name.toLowerCase() // rosy

 

 

 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함