22-04-27 JS 데이터

SOMEmo·2022년 4월 27일
1
post-thumbnail
  1. 문자
String.prototype.indexOf()
const result = "Hello world!'.indexOf('world')
console.log(result);
주어진 값과 일치하는 첫 번째 인덳를 반환

문자의 길이

str.length

문자열 일부 추출

const str= 'Hello world'

console.log(str.slice(0, 3)) //Hel

문자열 대체

console.log(str.replace('world', 'HEROPY'))
console.log(str.replace('world', ''))

match

정규표현식 이용
const str = 'thesecon@gmail.com
console.log(str.match(/.+(?=@)/)[0]) // thesecon

공백제거

str.trim()

  1. 숫자와 수학
const pi = 3.14159265358979
const str = pi.toFixed(2) // 3.14 문자데이터로 반환

parseInt(str) 정수로 반환
parseFloat(str)  실수 반환

절대값 반환

Math.abs(-12) //12

가장 작은 값

Math.min(2,8))

올림,내림, 반올림

Math.ceil(3.14)  //4
floor내림, round반올림

랜덤

Math.random() 

  1. 배열(1)
    Array mdn

find 메서드는 주어진 판별 함수를 만족하는 첫번째 요소의 값을 반환

const array1 = [5, 12, 8, 130, 44];
array1.find(elemnt => element > 10); //12

concat() 두개의 배열 데이터를 병합

numbers.concat(fruits)

forEach😫배열데이터의 item 개수만큼 특정 콜백함수를 실행. 따로 반환되는 값은 없다.(undefined)

fruits.forEach(function (element, index, array) {
  console.log(element, index, array)
})

map 콜백에서 반환된 새로운 배열 반환 😫

const b = fruits.map(function (fruit, index) {
  return `${fruit}-${index}`
})

  1. 배열(2)
//.filter() 특정 기준안에서 true인 경우 배열로 필터링
const b= numbers.filter(number => {
  return number < 3
}) // [1, 2]

.find() 조건에 맞는 아이템을 찾으면 찾는 걸 끝내고 값을 반환, .findindex()

const a = fruits.find(fruit => {
  return /^B/.test(fruit)    정규표현식 B로 시작하는 문자데이터   //Banana
})

const a = fruits.findIndex(fruit => {
  return /^C/.test(fruit)    //2
})

.includes() 배열에 인수가 포함되어 있는지 확인

const a = numbers.includes(3)  // true

.push() .unshift()
원본 수정됨 주의!

const numbers = [1,2,3,4]
const fruits = ['Apple', 'Banana', 'Cherry']

numbers.push(5)  // [1,2,3,4,5] 배열의 가장뒤쪽에 데이터 삽입
numbers.unshift(0) // [0,1,2,3,4,5] 배열의 맨앞에 데이터 삽입

.reverse()
원본 수정됨 주의!

numbers.reverse()  [4,3,2,1] 배열데이터를 거꾸로 나열한다.

.splice()
원본 수정됨 주의!

numbers.splice(2, 1) //[1,2,4] 배열데이터 인덱스, 삭제할 아이템 개수
numbers.splice(2,0,999) // [1,2,999,3,4] 인덱스 2번에 999추가
numbers.splice(2,1,99) // [1,2,99,4]

  1. 객체
    object mdn

Object.assign() 열거할 수 있는 하나 이상의 출처 객체로부터 대상 객체로 속성을 복사할 때 사용.

const target = {a:1, b:2};
const source = {b:4, c:5};

const returnedTarget = Object.assign(target, source);

console.log(target); //Object {1:1, b: 4, c:5}
console.log(returnedTarget); //Object {1:1, b: 4, c:5}
console.log(target === returnedTarget);

Object.keys()

const keys = Object.keys(user) //['name', 'age', 'email']
const values = keys.map(key => user[key]) // ["Heropy", 85, "thesecon@gmail.com"]

2개의 댓글

comment-user-thumbnail
2022년 4월 27일

유익하네요 :)

1개의 답글