// Object.assign()
// 하나 이상의 출처(Source) 객체로부터 대상(Target) 객체로 속성을 복사하고 대상 객체를 반환
const target = {a:1 , b:2}
const source1 = {b:3 , c:4}
const source2 = {c:5 , d:6}
//const ressult = Object.assign({},target,source1,source2)
const result = {
...target,
...source1,
...source2
}
console.log(target)
console.log(ressult)
// 객체 데이터는 속성이 고유!
// Object.entries()
// 주어진 객체의 각 속성과 값으로(키와 밸루) 하나의 배열 만들어 요소로 추가한 2차원 배열 반환
const user = {
name: 'Heropy',
age: 85,
isValid : true,
email: 'wjdwlswn23@naver.com'
}
console.log(Object.entries(user))
for (const [key , value] of Object.entries(user)) {
console.log(key, value)
}
Object.keys()
// 주어진 객체의 속성 이름을 나열한 배열을 반환
const user2 = {
name: 'Heropy',
age: 85,
isValid : true,
email: 'wjdwlswn23@naver.com'
}
console.log(Object.keys(user2))
// 주어진 객체의 속성 값을 나열한 배열을 반환
const user = {
name: 'Heropy',
age: 85,
isValid : true,
email: 'wjdwlswn23@naver.com'
}
console.log(Object.entries(user))
Json(JavaScript Object Notation)
- 데이터 전달을 위한 표준 포맷!
- 문자, 숫자, 불린, Null, 객체, 배열 데이터만 사용
- 문자는 큰 따옴표만 사용
- 후행 쉼표 사용불가
ex) const a = {
x:1,
y:2,(후행 쉼표 X)
}- .json 확장자 사용
- JSON.stringify() - 데이터를 JSON 문자로 반환
- JSON.parse() - JSON 문자를 분석해 데이터로 변환
console.log(JSON.stringify('Hello world!')) // "Hello world!"
console.log(JSON.stringify(123)) // 123 (얘는 문자임)
console.log(JSON.stringify(false))
console.log(JSON.stringify(null))
console.log(JSON.stringify(false))
console.log(JSON.stringify({naem: 'Heropy', age: 85}))
// {"name" : "Heropy", "age": "85"}
console.log(JSON.stringify([1,2,3])) [1,2,3]
console.log('//----------------------------------//')
console.log(JSON.parse('"Hello world!"'))
console.log(JSON.parse('123'))
console.log(JSON.parse('false'))
console.log(JSON.parse('null'))
console.log(JSON.parse('false'))
console.log(JSON.parse('{"name" : "Heropy", "age": "85"} '))
console.log(JSON.parse('[1,2,3]'))
--> 출력된 결과는 전부 json 문자가 아니라 실제 js 데이터인데,
main.js에서 import를 통해서 json 파일을 가져와 사용하게 되면, 현재 설치에서 사용하고 있는
parcel 번들러를 통해서 자동으로 분석이 돼서 js 데이터로 이용할 수 있는 것이다.
--> 하나의 json 확장자를 가지고 있는 파일이 곧 하나의 json 데이터이다.
--> 하나의 파일에서는 여러 개의 데이터를 만들 수 없다.
--> 배열 데이터 두개, 숫자, null, 등 하나의 파일에서 여러 개의 데이터를 작성할 수 없다.