๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ฌ/ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€

22์ผ์ฐจ

๐Ÿธ

22์ผ์ฐจ

 

Part 4. JavaScript Level up

Ch 2. JS ๋ฐ์ดํ„ฐ ์‹ค์Šต

 


 

Ch 2. JS ๋ฐ์ดํ„ฐ ์‹ค์Šต

 

 

๋‚ด์šฉ

 

 ๐Ÿ”— ๊ฐ€์ ธ์˜ค๊ธฐ, ๋‚ด๋ณด๋‚ด๊ธฐ 

// main.js

import _ from "lodash";
import checkType from "./getType";
// import { random, user as heropy } from "./getRandom";
import * as R from "./getRandom";

console.log(_.camelCase("the hello world"));
console.log(checkType([1, 2, 3]));
// console.group(random(), random());
console.log(R);

 

// getRandom.js
// default ์‚ฌ์šฉ ์•ˆ ํ–ˆ์„ ๋•Œ

export function random() {
  return Math.floor(Math.random() * 10);
}
export const user = {
  name: "HEROPY",
  age: 85,
};
export default 123;

 

// getType.js
// default ์‚ฌ์šฉ ํ–ˆ์„ ๋•Œ
// 2๊ฐœ ์ด์ƒ ์ •์˜ ๋ถˆ๊ฐ€

export default function (data) {
  return Object.prototype.toString.call(data).slice(8, -1);
}

 

 

 

 ๐Ÿ”— lodash 

https://lodash.com/docs/

 

Lodash Documentation

_(value) source Creates a lodash object which wraps value to enable implicit method chain sequences. Methods that operate on and return arrays, collections, and functions can be chained together. Methods that retrieve a single value or may return a primiti

lodash.com

 

_.unipBy(): ํ•˜๋‚˜์˜ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์œ ํ™” (์ค‘๋ณต ์ œ๊ฑฐ)

_.unionBy(): ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์œ ํ™” (์ค‘๋ณต ์ œ๊ฑฐ)

// main.js

import _ from "lodash";

const usersA = [
  { userId: "1", name: "HEROPY" },
  { userId: "2", name: "YUJIN" },
];

const usersB = [
  { userId: "1", name: "HEROPY" },
  { userId: "3", name: "GANG" },
];

const usersC = usersA.concat(usersB);
console.log("concat", usersC);
console.log("unipBy", _.uniqBy(usersC, "userId"));

const usersD = _.unionBy(usersA, usersB, "userId");
console.log("unionBy", usersD);

 

 

_.find(): ๋ฐฐ์—ด์—์„œ ํŠน์ •ํ•œ ๊ฐ์ฒด ์ฐพ๊ธฐ

_.findIndex(): ๋ฐฐ์—ด์—์„œ ํŠน์ •ํ•œ ๊ฐ์ฒด ์ธ๋ฑ์Šค ์ฐพ๊ธฐ

_.remove(): ๋ฐฐ์—ด์—์„œ ํŠน์ •ํ•œ ๊ฐ์ฒด ์‚ญ์ œ

// main.js

import _ from "lodash";

const users = [
  { userId: "1", name: "one" },
  { userId: "2", name: "two" },
  { userId: "3", name: "three" },
  { userId: "4", name: "four" },
  { userId: "5", name: "five" },
];

const foundUser = _.find(users, { name: "three" });
const foundUserIndex = _.findIndex(users, { name: "three" });
console.log(foundUser);
console.log(foundUserIndex);

_.remove(users, {name: 'one'})
console.log(users)

 

 

 

 ๐Ÿ”— JSON: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ํ‘œ๊ธฐ๋ฒ• 

https://ko.wikipedia.org/wiki/JSON

 

JSON - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

JSON(์ œ์ด์Šจ[1], JavaScript Object Notation)์€ ์†์„ฑ-๊ฐ’ ์Œ( attribute–value pairs and array data types (or any other serializable value)) ๋˜๋Š” "ํ‚ค-๊ฐ’ ์Œ"์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ๊ฐ„์ด ์ฝ์„ ์ˆ˜ ์žˆ

ko.wikipedia.org

JSON์€ {"ํ‚ค": ๊ฐ’} ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

// main.js

import myData from "./myData.json";

console.log(myData);

const user = {
  name: "YUJIN",
  age: 23,
  emails: ["hyj@gamil.com", "hyj@naver.com"],
};
console.log("user", user);

// ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹(๋ฌธ์ž ๋ฐ์ดํ„ฐ)์œผ๋กœ
const str = JSON.stringify(user);
console.log("str", str);
console.log(typeof str);

// JSON ํ˜•์‹(๋ฌธ์ž ๋ฐ์ดํ„ฐ)์„ ๊ฐ์ฒด(์‹ค์ œ ๋ฐ์ดํ„ฐ)๋กœ ์žฌ์กฐ๋ฆฝ
const obj = JSON.parse(str);
console.log("obj", obj);

 

JSON์€ ๋ฌธ์žํ˜•์ด๋‹ค.

 

 

 ๐Ÿ”— Storage 

https://developer.mozilla.org/ko/docs/Web/API/Window/localStorage

 

Window.localStorage - Web API | MDN

localStorage ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด Document ์ถœ์ฒ˜์˜ Storage ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ธŒ๋ผ์šฐ์ € ์„ธ์…˜ ๊ฐ„์— ๊ณต์œ ๋ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

localStorage์˜ setItem์€ ๋ฌธ์ž ํ˜•์‹์œผ๋กœ ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

// main.js

const user = {
  name: "YUJIN",
  age: 23,
  emails: ["hyj@gmail.com", "hyj@naver.com"],
};

// localStorage.setItem("user", JSON.stringify(user));
// console.log(JSON.parse(localStorage.getItem("user")));
// localStorage.removeItem("user");

const str = localStorage.getItem("user");
const obj = JSON.parse(str);
obj.age = 100;
console.log(obj);
localStorage.setItem("user", JSON.stringify(obj));

 

 

๐Ÿ”— lowdb

https://github.com/typicode/lowdb

 

GitHub - typicode/lowdb: Simple to use local JSON database. Powered by plain JavaScript (supports Node, Electron and the browser

Simple to use local JSON database. Powered by plain JavaScript (supports Node, Electron and the browser) - GitHub - typicode/lowdb: Simple to use local JSON database. Powered by plain JavaScript (s...

github.com

 

 

 ๐Ÿ”— OMDb API 

 

http://www.omdbapi.com/

 

OMDb API - The Open Movie Database

 

www.omdbapi.com

 

๐Ÿ”— Query String

์ฃผ์†Œ?์†์„ฑ=๊ฐ’&์†์„ฑ=๊ฐ’&์†์„ฑ=๊ฐ’

 

๐Ÿ”— axios

https://github.com/axios/axios

 

GitHub - axios/axios: Promise based HTTP client for the browser and node.js

Promise based HTTP client for the browser and node.js - GitHub - axios/axios: Promise based HTTP client for the browser and node.js

github.com

axios
  .get() // ์š”์ฒญ
  .then() // ์‘๋‹ต ์ฒ˜๋ฆฌ

 

// main.js

import axios from "axios";

function fetchMovies() {
  axios.get("https://www.omdbapi.com/?apikey=7035c60c&s=frozen").then((res) => {
    console.log(res);
    const h1El = document.querySelector("h1");
    const imgEl = document.querySelector("img");
    h1El.textContent = res.data.Search[0].Title;
    imgEl.src = res.data.Search[0].Poster;
  });
}

fetchMovies();