TIL 29/04/21 (객체 for key in object, every(), includes())

Seonmi Choi·2021년 4월 28일
0

Start again!!!

목록 보기
9/40

I really embarraced a lot!!!!
The error was a typing error!!!
I wondering why the method was so out of reach.

css tip
transition: transform(.3s) - 동작을 부드럽게 animation

for...in 문과 for...of 문은 배열, 객체, 문자열 등과 같은 반복 가능한 객체의 값을 하나씩 가져와 반복할 수 있게끔 한다.

object with all key/value pairs
removed for which the value holds an odd number.

let object  = { a: 1, b: 2, c: 3, d: 'hello' }
function removeOddValues(object) {
  for (let key in object) {
    if(typeof object[key]=== 'number')
    if(object[key]%2 !== 0) {
      console.log(object[key]);
      delete object[key]; 
    }
    
  }
  return object;
  
}
removeOddValues(object);
 1 // console.log(object[key]);
 3 // console.log(object[key]);
{b: 2, d: "hello"}

tip : 배열이나 문자열에서 i값을 정할때 i값이 number값이 아닌 string으로 인식을 하기때문에 undefined라고 뜬다. +i처럼 i 앞에 +를 붙여주면 i를 숫자로 인식한다.

const arr = ['one', 'two', 'three'];

for (const i in arr) {
  console.log(arr[i + 1]);
} //undefined
const arr = ['one', 'two', 'three'];

for (const +i in arr) {
  console.log(arr[+i + 1]);// + makes the big changes
} // two
  // three

array.prototype.every

[12, 5, 8, 130, 44].every(elem => elem >= 10); // false
[12, 54, 18, 130, 44].every(elem => elem >= 10); // true

array.protype.includes

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

what is bubblesort??
여러 정렬 알고리즘(삽입 정렬, 퀵 정렬, 병합 정렬, 기수 정렬 등) 중 가장 기본적인 알고리즘
정렬하는 모습이 거품이 꺼지는 듯한 모습과 비슷하다고 해서 이름 붙여짐
요소 하나하나를 돌면서 크기를 비교하니 성능이 엄청 안좋음

Primitive type vs Reference type??
자바스크립트에서는 원시 타입(primitive type) 참조 타입(reference type)이라는 두 가지 자료형을 제공

숫자, 불린값, null과 undefined는 원시 타입이다. 문자열도 원시타입처럼 동작한다. 객체, 배열, 함수는 참조 타입이다.

profile
I am not afraid of learning!

0개의 댓글