๐ JSON ๊ฐ์ฒด ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ธฐ(depth ์ค์ด๊ธฐ)
fetch
๋ axios
๋ฅผ ์ด์ฉํด์ json
ํํ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ฌ ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฐ์ดํฐ๊ฐ ๋์ ๋ถ(?)ํ ๋๊ฐ ์๋ค.
let users = [
// ์ฒซ๋ฒ์งธ ๊ฐ์ฒด
{
gender: "female",
name: {
title: "Mrs",
first: "Emília",
last: "Vieira",
},
location: {
street: {
number: 3944,
name: "Rua Santa Luzia ",
},
city: "Indaiatuba",
state: "Acre",
country: "Brazil",
postcode: 26038,
coordinates: {
latitude: "23.2809",
longitude: "-115.5400",
},
timezone: {
offset: "+10:00",
description: "Eastern Australia, Guam, Vladivostok",
},
},
email: "emilia.vieira@example.com",
login: {
uuid: "b2571400-3882-4338-93df-6d4fb09a0d06",
username: "brownpeacock605",
password: "father",
salt: "fRwNiuz7",
md5: "5f594b1e31c96ffd5a40bf7efed2e4e3",
sha1: "3634458cf569467a5f2cdd46ec3c6a86a9f12b04",
sha256: "d0b25f5502e1a88b49017833b5247629150a6326d2870d9af63bdd595217e138",
},
dob: {
date: "1981-06-20T06:08:31.406Z",
age: 40,
},
registered: {
date: "2019-04-12T21:34:33.551Z",
age: 2,
},
phone: "(20) 9022-5571",
cell: "(32) 5117-1461",
id: {
name: "",
value: null,
},
picture: {
large: "https://randomuser.me/api/portraits/women/4.jpg",
medium: "https://randomuser.me/api/portraits/med/women/4.jpg",
thumbnail: "https://randomuser.me/api/portraits/thumb/women/4.jpg",
},
nat: "BR",
},
// ๋๋ฒ์งธ ๊ฐ์ฒด
{
gender: "female",
name: {
title: "Mrs",
first: "Anne",
last: "Burke",
},
location: {
street: {
number: 8567,
name: "Depaul Dr",
},
city: "York",
state: "Kentucky",
country: "United States",
postcode: 98943,
coordinates: {
latitude: "14.8362",
longitude: "65.8872",
},
timezone: {
offset: "-3:30",
description: "Newfoundland",
},
},
email: "anne.burke@example.com",
login: {
uuid: "5f26d53c-17da-4a21-83a8-539fc83c5d55",
username: "sadelephant909",
password: "baxter",
salt: "CYSz1FYq",
md5: "573f67237e881ed9dd07bacab90080df",
sha1: "8b95f7fced02ec9aba752bb2ebd7fcab35da9256",
sha256: "23e52b0456fdf03fdabd8a3945728d08ce029e7633d3bd6317ba41c157cd16dc",
},
dob: {
date: "1977-01-14T23:06:16.613Z",
age: 44,
},
registered: {
date: "2007-05-13T19:29:17.788Z",
age: 14,
},
phone: "(741)-381-4863",
cell: "(793)-695-9075",
id: {
name: "SSN",
value: "922-18-5994",
},
picture: {
large: "https://randomuser.me/api/portraits/women/85.jpg",
medium: "https://randomuser.me/api/portraits/med/women/85.jpg",
thumbnail: "https://randomuser.me/api/portraits/thumb/women/85.jpg",
},
nat: "US",
}
];
๋ฐ์ดํฐ ์์ฒด๋ก ํ์ํ ๊ฐ์ฒด๋ง ๋ฝ์์ ์๋จ์ ํ์ํด๋ ๋์ง๋ง, ๊ฐ์ฒด๋ฅผ ์ง๊ด์ ์ด๊ณ ํ๋์ ๋ด๋ ๊น๋ํ๊ฒ ๋ณด์ด๊ฒ๋ ๋ฐ๊พธ๊ณ ์ถ๋ค๋ ๊ฐ๋ฐ๊ฐ์ด ๋ค์ง ์๋๊ฐ? ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง, ํจ์๋ฅผ ๋ง๋ค์ด์ ํ์ํ ๊ธฐ๋ฅ๋ณ๋ก ์ญํ ์ ๋๋ 1depth
์ ๊ฐ์ฒด๋ก ๋ฐ๊พธ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค. ๋จผ์ fetchUsers()
ํจ์๋ก fetch()
๊ธฐ๋ฅ๋ง ๋ด๋นํ๋ ํจ์๋ฅผ ๋ง๋ค๊ณ , getOptimizeData()
ํจ์๋ก 1depth
์ ๊ฐ์ฒด๋ก ๋ฐ๊พผ๋ค. ๋ง์ง๋ง์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ responseUsers()
ํจ์๋ฅผ ๋ง๋ค์๋ค. ์ฒ์ ๋ฐ์ ๋ฐ์ดํฐ์ map
ํจ์๋ก ๊ฐ์ฒด ํ๋์ฉ ๊บผ๋ด์ getOptimizeData
ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์๋ค ๊ฒฐ๋ก ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ๋ฐ๋์๋ค. ์ธ์ ๊ฐ json
์ผ๋ก ๋ฐ์ ๊ฐ์ฒด๋ฅผ ์ ๋ฆฌํ๋ ์ผ์ด ์๊ธฐ๋ฉด ์ง๊ธ๊ณผ ๊ฐ์ ํจํด์ผ๋ก ์ ์ฉํ๊ฒ ์จ๋จน์ ์ ์์ ๊ฒ ๊ฐ์ ๊ธ์ ๋จ๊ธด๋ค.
// ๊ฐ์ฒด ์ต์ ํ
function getOptimizeData(users) {
const email = users.email;
const name = `${users.name.first} ${users.name.last}`;
const pictureUrl = users.picture.large;
const username = users.login.username;
const location = `${users.location.country}, ${users.location.state}, ${users.location.city}`;
const age = users.dob.age;
return {
email,
name,
pictureUrl,
username,
location,
age,
};
}
// fetch ํจ์
const fetchUsers = () => {
return fetch("https://randomuser.me/api/?results=10")
.then((user) => user.json())
.then((data) => data.results);
};
// ๊ฒฐ๊ณผ ๋ฐํ
const requestUsers = () => {
return fetchUsers().then((users) => {
return users
.map((user) => getOptimizeData(user))
});
};
๐๐ฝ
[
{
email: "kreszenz.linnemann@example.com",
name: "Kreszenz Linnemann",
pictureUrl: "https://randomuser.me/api/portraits/women/28.jpg",
username: "purplesnake729",
location: "Germany, Bayern, Waldkirch",
},
{
email: "kelly.wood@example.com",
name: "Kelly Wood",
pictureUrl: "https://randomuser.me/api/portraits/women/74.jpg",
username: "brownzebra447",
location: "United Kingdom, Derbyshire, Bangor",
},
{
email: "romy.guerin@example.com",
name: "Romy Guerin",
pictureUrl: "https://randomuser.me/api/portraits/women/37.jpg",
username: "smallelephant251",
location: "France, Vosges, Angers",
},
{
email: "frida.petersen@example.com",
name: "Frida Petersen",
pictureUrl: "https://randomuser.me/api/portraits/women/75.jpg",
username: "greenmouse281",
location: "Denmark, Danmark, Sørvad",
},
{
email: "deann.barnes@example.com",
name: "Deann Barnes",
pictureUrl: "https://randomuser.me/api/portraits/women/14.jpg",
username: "greenpeacock642",
location: "Australia, New South Wales, Warrnambool",
},
{
email: "zara.brown@example.com",
name: "Zara Brown",
pictureUrl: "https://randomuser.me/api/portraits/women/31.jpg",
username: "redgorilla850",
location: "New Zealand, Marlborough, Whanganui",
},
{
email: "kaja.guttormsen@example.com",
name: "Kaja Guttormsen",
pictureUrl: "https://randomuser.me/api/portraits/women/88.jpg",
username: "goldenostrich157",
location: "Norway, Nord-Trøndelag, Eide",
},
];
๋๊ธ