getQueryObject

쿼리를 Object로 반환해주는 함수

const getQueryObj = <T extends {}> (search: string): T => {
  const arr = window.location.search.substr(1).split('&');
  return arr.reduce((prev, curr) => {
    const split = curr.split('=');
    return { ...prev, [split[0]]: split[1] }
  }, {} as T);
}

http://example.com/[email protected]&name=helloworld

const queryObj = getQueryObj<{ email: string, name: string }>(window.location.search);
// { email: '[email protected]', name: 'helloworld' }

좋은 웹페이지 즐겨찾기