본문 바로가기
카테고리 없음

js2022-11-17 게시판, 배열 forEach, filter

by 김건영 2022. 11. 20.
const person = {
    name: `ingoo`,
    age: 102
}

console.log(person)

const person2 = JSON.stringify(person)
// string으로 만들어준다.
console.log(person2)

console.log(typeof person) //object
console.log(typeof person2) // string

const str = `{"name":"ingoo","age":102}`

console.log(person2 === str)  //true

const person3 = JSON.parse(person2)
// object로 만들어준다.
console.log(person3)

JSON.stringify()     object -> string                               JSON.parse()   string -> object

페이지가 바뀔때마다 데이터를 공유를 할수가없으니 그 부분을 브라우저에 파일을 생성해서 저장시킨다. 브라우저에서 저장시키는 것은 자스의 데이터타입들이 아니라 결국은 글자(string) 밖에안된다.  그럴때  위 방법과같이 스트링으로 변환시키고 받아와서 다시 객체로 만들어주는 작업을 한다. 

 

웹 스토리지
    - 로컬 스토리지 - 직접삭제하기전까지 삭제가 안된다.
    - 세션 스토리지 - 브라우저 종료하면 삭제된다

쿠키
    - 데이터 저장하는 용량이 적다. 최대(4Kb)
같은 메커니즘

window.localStorage.setItem(`name`,`ingoo`)
데이터를 입력하고싶으면 setItem 을 호출하면된다. 키, 값
window.localStorage.getItem(`name`,)
가져오려면 키만 호출하면 된다.

 

const arr = [1, 2, 3, 4, 5]
arr.forEach((v, i)=>{ console.log(v, i) })

애로우함수 이용

v 는  value값    i 는 index번호        답은  1 0, 2,1  ~~~~~~

 

const arr= [2,7,5,4,5]
const arr2 = arr.filter((v)=>{return v ===5})
console.log(arr2)

애로우함수 이용

 v  value값  이  5 이면  반환한다. 새로운 arr2에    답은 [5, 5]

 

댓글