배열과 객체의 구조를 이해하고 언제, 어떻게 사용하는지 이해할 수 있다.
객체 속성(property)의 추가, 조회, 변경, 삭제를 자유자재로 할 수 있다.
배열과 객체, 반복문을 응용하여 능숙하게 대량의 정보를 다룰 수 있다.
let user = {
firstName: 'Steve',
lastName: 'Lee',
email: 'steve@codestates.com',
city: 'Seoul'
};;
//Dot notation으로
//Steve라는 값에 접근 하려면 ?
user.firstName; //'Steve'
//Seoul이라는 값에 접근 하려면?
user.city;// 'Seoul'
let user = {
firstName: 'Steve',
lastName: 'Lee',
email: 'steve@codestates.com',
city: 'Seoul'
};;
//Bracket notation으로
//Steve라는 값에 접근 하려면 ?
user['firstName']; //'Steve'
//Seoul이라는 값에 접근 하려면?
user['city'];// 'Seoul'
//firstname / city 키가 문자열로 들어가서 ''를써야한다.
let tweet = {
wrtier: 'stevelee',
createdAt: '2019-09-10 12-03:33',
content: '프리코스 재밌어요!'
};
->위와 같은 객체가 있다.
-> Bracket notation으로 content라는 키의 값을 가져오려고 하는데
->tweet[content]
라고 적었을때는 에러가 발생한다. (에러이유: content가 정의(할당)되지 않았다)
-> 이와 같은 상황은 지금 content를 변수로 취급하고 있기 때문에 발생하는 것이다.
그러기 때문에 , content라는 변수에 'content'라는 문자열을 담아주면 문제가 해결된다.
keyname이라는 변수에'content'라는 문자열을 담아 줫을 경우
-> let keyname = 'content';
(keyname을 content라는 변수에 할당한다.)
tweet[keyname]
=== tweet['content']
와 같아진다.
function getProperty(obj, propertyName){
return obj[propertyName]; // 1번요구사항 : obj['name'] // 2번요구사항 : obj['age']
let tweet = {
wrtier: 'stevelee',
createdAt: '2019-09-10 12-03:33',
content: '프리코스 재밌어요!'
};
tweet['category']= '잡담'; //bracket notation으로 문자열을 추가
tweet.isPublic = true; // dot notation 으로 boolean값을 추가
tweet.tags = ['#코드스테이츠', '#프리코스']; // dot notation 으로 배열을 추가
let tweet = {
wrtier: 'stevelee',
createdAt: '2019-09-10 12-03:33',
content: '프리코스 재밌어요!'
};
// createdAt 키-값 쌍을 지울때
delete tweet.createdAt;
// tweet = {
wrtier: 'stevelee',
content: '프리코스 재밌어요!'
};
let tweet = {
wrtier: 'stevelee',
createdAt: '2019-09-10 12-03:33',
content: '프리코스 재밌어요!'
};
'content' in tweet; // true
'updatedAt' in tweet; // false
출처: 코드스테이츠