๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Frontend/JavaScript

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] JSON ๊ฐ์ฒด ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ธฐ(depth ์ค„์ด๊ธฐ)

by YWTechIT 2021. 11. 18.
728x90

๐Ÿ“ 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",
    }
];

 

728x90

 

๋ฐ์ดํ„ฐ ์ž์ฒด๋กœ ํ•„์š”ํ•œ ๊ฐ์ฒด๋งŒ ๋ฝ‘์•„์„œ ์•ž๋‹จ์— ํ‘œ์‹œํ•ด๋„ ๋˜์ง€๋งŒ, ๊ฐ์ฒด๋ฅผ ์ง๊ด€์ ์ด๊ณ  ํ•œ๋ˆˆ์— ๋ด๋„ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ด๊ฒŒ๋” ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋Š” ๊ฐ•๋ฐ•๊ฐ์ด ๋“ค์ง€ ์•Š๋Š”๊ฐ€? ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ, ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋ณ„๋กœ ์—ญํ• ์„ ๋‚˜๋ˆ  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",
    },
];
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€