배열(Array)이란?
const name = "june";
같은 value 를 여러개 가지고 있는 것
const animals = ["lion", "bear", "monkey"};
★ 배열의 value는 데이터 타입 상관 없이 저장이 가능하다
배열의 value 출력
const animals = ["lion", "bear", "monkey"}; console.log(animals[1]); //bear가 출력이 됨. []를 사용하면 0,1,2... 순서로 출력
배열의 value 조작 - 직접 추가
const animals = ["lion", "bear", "monkey"}; animals[3] = "dear"; console.log(animals); //(4) ["lion", "bear", "monkey", "dear"]
배열의 value 조작 - push( ), unshift( ), pop( ), splice( ) 메서드 사용
//push : 배열의 value 추가 const animals = ["lion", "bear", "monkey"}; animals.push("dear"); console.log(animals); //(4) ["lion", "bear", "monkey", "dear"]
//unshift : 맨 앞에 value 추가 const animals = ["lion", "bear", "monkey"}; animals.unshift("hippo"); console.log(animals); //(4) ["hippo", "lion", "bear", "monkey"]
//pop : 맨 뒤에 value 삭제 const animals = ["lion", "bear", "monkey"}; animals.pop(); console.log(animals); //(2) ["lion", "bear"]
//splice : 배열의 특정 value 지정 삭제 //(a,b) : a_몇번쨰 배열의, b_몇개 const animals = ["lion", "bear", "monkey"}; animals.splice(1, 1); console.log(animals); //(2) ["lion", "monkey"] animals.splice(1, 2); console.log(animals); //(1) ["lion"]
★ 배열의 기타 메서드
//isArray( ) : 해당 함수가 배열인지 확인하는 메서드 const animals = ["lion", "bear", "monkey"}; console.log(Array.isArray(animals)); //true console.log(Array.isArray("hello")); //false
//indexOf( ) : 배열의 순서를 확인하는 메서드 const animals = ["lion", "bear", "monkey"}; console.log(animals.indexOf('bear')); //[1]
객체(Object)란?
key와 value의 조합을 가지고 있는 것const mokey = { name: "ringo", face: "funny", age: 11, food: ["banana", "apple", "strawberry"], //object 안에 배열도 입력 가능 tail: true, //boolean 값도 사용 가능 location: { //object 안에 또다른 object 입력 가능 country: "congo", place: "forest", isAfrica: true, } } console.log(monkey); //{name: "ringo", face: "funny", age: 11 ...}
object의 value 출력
const mokey = { name: "ringo", face: "funny", age: 11, food: ["banana", "apple", "strawberry"], //object 안에 배열도 입력 가능 tail: true, //boolean 값도 사용 가능 location: { //object 안에 또다른 object 입력 가능 country: "congo", place: "forest", isAfrica: true, } } console.log(monkey.face); //funny console.log(monkey.food[1]); //apple console.log(monkey.location.place); //forest
object의 value 조작 - 직접 추가
const mokey = { name: "ringo", face: "funny", age: 11, food: ["banana", "apple", "strawberry"], //object 안에 배열도 입력 가능 tail: true, //boolean 값도 사용 가능 location: { //object 안에 또다른 object 입력 가능 country: "congo", place: "forest", isAfrica: true, } } monkey.name = "star"; //name의 value 값 변경 console.log(monkey.name); //star monkey.food[1] = "peanut"; console.log(monkey.food); //banana, peanut, strawberry monkey.weight = 50; //object key+value 직접 추가 console.log(monkey); //object에 weight : 50 이라는 key와 value가 생성