티스토리 뷰

codestates/section1

unit8[javaScript]배열,객체

나아눙 2022. 12. 29. 09:28

배열

let so = [ 1, 2, 3, 4, 5];

값은 요소 // 1, 2, 3, 4, 5

순서가 있는 값은인덱스 [0],[1].[2].[3],[4].[5]

so[0[ // 1 

so //[1,2,3,4,5]

 

배열길이

so.length //5

 

요소추가

so.push(6) 배열끝에 6을 추가 

요소삭제

so.push() 배열 마지막 값 삭제

 

배열의 반복

for( let n=0; n<so.length; n++)

{

   cconsole.log(so[n]);

}

배열의 합 

undefined+10 //NaN 

sum을 꼭 선언

 

배열 메서드

 

typeof

typeof so //object

typeof [1,2,3] //object

let ob = {a:5}

typeof ob; // object

Array.isArray(123] // false

Array.isArray([1,2,3]) ,Array.isArray([]) // True

 

push , pop

console.table() :시각화 하기 편함

arr.push : 기존 array 끝에다가 요소 추가

arr.pop() : 끝에 요소를 삭제

 

unshift,shift

arr.unshift : 0번째 맨앞 요소 추가

arr.shift() : 0번째 맨앞 요소 삭제

 

indexof

so.indexOf(2) //3 인덱스 값이 나온다.

so.indexOf(7) // -1 없는 값은 -1이 나온다.

 

so.indexOf(2) !-== -1 //true

so.indexOf(7) !== -1 //false

function hasElement(arr,element){

   let ishas = arr.indexOf(element)!==-1;

   return ishas;

}

 

includes

so.includes(2) //true

so.includes(7) //false

 

코플릿을 풀다가

원본 배열을 변경하는 메서드 : push ,pop,shift,unshift

원본 배열을 변경하기 때문에 그대로 return 해도 된다.

원본 배열을 변경하지 않는 메서드 : slice , concat

원본 배열을 변경하지 않기 때문에 원하는 답을 얻을려면 새로운 변수를 선언을 하고 return을 한다.

 

arr=[];

arr.split()

 

 

객체

객체는 키와 값 쌍으로 이루어짐

let so = {

    firstName : 's',

    lastName : 'o',

    email:'sy@~.com

    city:'Seoul'

};

 

Dot notation 

so.firstName

so.city

Bracket notation: 키값이 변수일때,키값이 변할 때

so['firstname']

so['city']

so["city"] 

so[`city`]

 

so['content'] 문자열을 담는다,키값을 가져온다.

so['content']  = so.content

so[content] 변수를 직접 가져온다, 

문자열을 넣거나 변하는 변수를 넣는다.

 

값 추가

so['category'] = '수다';

so.isPublic = true;

so.tags = ['hi','bye'];

 

키값 쌍을 지운다.

delete so.createAt

 

'city' in so; //true

'content' in so; //false

 

 

 

 

 

 

 

 

 

'codestates > section1' 카테고리의 다른 글

unit9[javascript]JavaScriptkonans 10문제  (0) 2023.01.04
unit9[javascript] 핵심개념과 주요문법  (0) 2023.01.03
unit7[Linux,git기초]  (0) 2022.12.28
unit5[html/css]활용  (0) 2022.12.23
Unit4 [css기초]  (0) 2022.12.22
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/09   »
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
글 보관함