object, array

Shin Woohyun·2021년 7월 27일
0

object

Literals and properties

const obj1 = {} //'object literal' syntax
const obj2 = new Object(); //'object constuctor' syntax

js는 동적으로 타입이 런타임때 결정된다. 그래서 나중에 key,value를 추가하거나 삭제할 수 있지만 되도록 안하는 것이 좋다.

Computed properties

person.name은 일반적으로, person['name']은 동적으로 키에 관련된 value를 받아와야 할 때 사용한다.

Property value shorthand

key, value가 같을 때 생략 가능.

function makePerson(name, age) {
  return {
    name,
    age,
  }
}

Constructor Function

function Person(name, age) {
  //this = {};
  this.name = name;
  this.age = age;
  }
  //return this;
}

in operator (key in obj)

for..in VS for..of

  • for (key in obj)
  • for (value of iterable)

Cloning

object를 복사해서 넣기
1. for..in으로 새로운 object에 복사
2. Object.assign(target, ...sources)


array

declaration

const arr1 = new Array();
const arr2 = [1, 2];

Looping over an array

  1. for
  2. for..of
  3. forEach
fruits.forEach(function(fruit, index) {
  console.log(fruit, index);
});

fruits.forEach((fruit, index) => console.log(fruit, index));

Addition, deletion, copy

push, pop vs unshift, shift
push, pop은 뒤에서, unshift, shift는 앞에서 넣고 빼는데 앞에서 넣고 빼면 뒤 인덱스가 모두 밀려가기 때문에 시간이 더 걸린다.

  • splice : remove an item by index position
    splice(index, remove-count, 추가할 컨텐츠)
  • concat : combine two arrays

Searching

  • indexOf
  • includes
  • lastIndexOf

https://youtu.be/1Lbr29tzAA8
https://youtu.be/yOdAVDuHUKQ

0개의 댓글