๐Ÿ“ ์˜ค๋Š˜ ํ•œ ๊ฒƒ

  1. React props - ์ปดํฌ๋„ŒํŠธ ์žฌ์„ค์ •

  2. underdash ์ฝ”๋“œ ๋ฆฌ๋ทฐ ์ •๋ฆฌ(2)


๐Ÿ“š ๋ฐฐ์šด ๊ฒƒ

1. PROPS

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•

1) ์ปดํฌ๋„ŒํŠธ ์žฌ์„ค์ •

style์€ ๊ฐ™๊ณ  text๋งŒ ๋‹ค๋ฅธ ๋ฒ„ํŠผ๋“ค์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

(1) ๋น„ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ผ์ผ์ด ๋ชจ๋“  ๋ฒ„ํŠผ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ style์„ ๋ณต๋ถ™ํ•ด์•ผ ํ•œ๋‹ค.
์‹ค์งˆ์ ์œผ๋กœ ๋‘ ๋ฒ„ํŠผ ์‚ฌ์ด์— ๋‹ค๋ฅธ ์ ์€ text ๋ฐ–์— ์—†๋‹ค.

const SaveBtn = () => {
  return (
    <div>
      <button
        style={{
          backgroundColor: "lightsalmon",
          color: "white",
          border: 0,
          borderRadius: 10,
          padding: "10px 20px",
        }}
      >
        Save
      </button>
    </div>
  );
};

const ConfirmBtn = () => {
  return (
    <div>
      <button
        style={{
          backgroundColor: "lightsalmon",
          color: "white",
          border: 0,
          borderRadius: 10,
          padding: "10px 20px",
        }}
      >
        Confirm
      </button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <SaveBtn />
      <ConfirmBtn />
    </div>
  );
};

const root = document.getElementById("root");
ReactDOM.render(<App />, root);

(2) React props ์ด์šฉ

๊ทธ๋Ÿฌ๋‚˜ React props๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์ธ App ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ธ Btn ์ปดํฌ๋„ŒํŠธ์— prop์„ ๋„˜๊ฒจ์คŒ์œผ๋กœ์จ ํ•˜๋‚˜์˜ ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

const Btn = (props) => { // {text, big}
  return (
    <div>
      <button
        style={{
          backgroundColor: "lightsalmon",
          color: "white",
          border: 0,
          borderRadius: 10,
          padding: "10px 20px",
          fontSize : props.big ? 18 : 16, // big ? 18 : 16
        }}
      >
        {props.text} // {text}
      </button>
    </div>
  );
};

const App = () => { // ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ
  return (
    <div>
      <Btn text="Save" big="true" /> // ์ž์‹ ์ปดํฌ๋„ŒํŠธ ์žฌ์„ค์ •
      <Btn text="Confirm" big="false" /> // ใ€ƒ
    </div>
  );
};

const root = document.getElementById("root");
ReactDOM.render(<App />, root);

์ปดํฌ๋„ŒํŠธ๋Š” ํ•ญ์ƒ Object ๊ฐ์ฒด ํ˜•์‹์˜ ํ•˜๋‚˜์˜ ์ธ์ž๋งŒ์„ ๋ฐ›๋Š”๋‹ค.
Btn ์ปดํฌ๋„ŒํŠธ์—์„œ console.log(props) ์„ ์‹คํ–‰ํ•˜๋ฉด {text: Save, big: true} {text: Confirm, big: false} ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
props ๋Œ€์‹ ์— shortcut ํ˜•ํƒœ์ธ {text , big} ์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

ํ•œํŽธ, ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ Boolean ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ(big)๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

(3) props๋กœ ํ•จ์ˆ˜ ์ „๋‹ฌ

์•ž์„œ props๋กœ string๊ณผ boolean ๊ฐ’์„ ์ „๋‹ฌํ–ˆ๋Š”๋ฐ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Btn์˜ text๋ฅผ App์˜ state์— ์—ฐ๊ฒฐํ–ˆ๋‹ค.
state๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค Btn์˜ text prop์˜ ๊ฐ’์ด ๋ฐ”๋€Œ๋„๋ก App ์ปดํฌ๋„ŒํŠธ๊ฐ€ return ํ•˜๋Š” Btn ์ปดํฌ๋„ŒํŠธ์— changeValue prop์„ ์ถ”๊ฐ€ํ•œ ํ›„ Btn ์ปดํฌ๋„ŒํŠธ์— ์ธ์ž๋กœ changeValue prop์„ ์ „๋‹ฌํ–ˆ๋‹ค.

๐Ÿ’ก ์ฃผ์˜!

App ์ปดํฌ๋„ŒํŠธ๊ฐ€ return ํ•˜๋Š” ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ Btn์— changeValue={changeValue}์„ ์ถ”๊ฐ€ํ•ด๋„ ์ด๊ฒƒ์€ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์•„๋‹ˆ๋ผ Btn์— ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” prop์ผ ๋ฟ์ด๋‹ค.
onClick={changeValue}๋ผ๊ณ  ํ•ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€, onClick ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์™€ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ–์ง€๋Š” ์—ฌ๊ธฐ์„œ์˜ onClick์€ ๊ทธ์ € prop์ผ ๋ฟ์ด๋‹ค.

Btn ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ HTML ์š”์†Œ button์— onClick={changeValue}์„ ์ถ”๊ฐ€ํ•ด์•ผ ์ด๊ฒƒ์ด ์‹ค์ œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋œ๋‹ค.
onClick={onClick}๋ผ๊ณ  ํ•ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€, ์•ž์˜ onClick์€ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ, ๋’ค์˜ onClick์€ prop์ด๋‹ค.

const Btn = ({text, changeValue}) => {
  return (
    <div>
      <button
        onClick={changeValue}
        // ์ค‘๋žต
        }}
      >
        {text}
      </button>
    </div>
  );
};

const App = () => {
  const [value, setValue] = React.useState("Save Changes");
  const changeValue = () => setValue("Revert Changes");
  return (
    <div>
      <Btn text={value} changeValue={changeValue} />
      <Btn text="Continue" />
    </div>
  );
};

(4) React.memo()

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์–ด๋–ค state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ๋‹ค๋ฅธ state ๊ฐ’์„ prop์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด๋„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state ๋ณ€๊ฒฝ์— ์˜ํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ๋˜ํ•œ ๊ฐ™์ด ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค.

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ธฐ์–ตํ•˜๋„๋ก ํ•จ์œผ๋กœ์จ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋”๋ผ๋„ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ prop์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฆฌ๋ Œ๋”๋ง ๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด React.memo()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const Btn = ({text, changeValue}) => {
  // ์ค‘๋žต
};

const MemorizedBtn = React.memo(Btn); // ์ถ”๊ฐ€

const App = () => {
  // ์ค‘๋žต
  return (
    <div>
      <MemorizedBtn text={value} changeValue={changeValue} /> // ์ˆ˜์ •
      <MemorizedBtn text="continue" /> // ์ˆ˜์ •
    </div>
  );
};

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์ˆ˜๋ฐฑ ์ˆ˜์ฒœ ๊ฐœ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•ญ์ƒ ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค๋ฉด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋Š๋ ค์ง€๋Š” ์›์ธ์ด ๋  ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด ์ด๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

(5) PropTypes

Typecheking With PropTypes ์ฐธ๊ณ 

ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งค์šฐ ๋งŽ์€ prop์„ ๊ฐ€์งˆ ๋•Œ ์ž˜๋ชป๋œ prop์„ ์ „๋‹ฌํ•˜๋Š” ๋“ฑ์˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, text prop์˜ ๊ฐ’์œผ๋กœ string์ด ์•„๋‹ˆ๋ผ number๋ฅผ ์ ์–ด์ค˜๋„ ์ฝ”๋“œ ์ƒ์œผ๋กœ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค.

// propType ์„ค์น˜ (์—ฌ๊ธฐ์„  Node.js ์‚ฌ์šฉ x)
<script src="https://unpkg.com/prop-types@15.7.2/prop-types.js"></script>

propTypes ํŒจํ‚ค์ง€๋Š” ์„ค์น˜ํ•˜๋ฉด props๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ด์–ด์•ผ ํ•˜๋Š”์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
์„ค์ •ํ•œ ํƒ€์ž…๊ณผ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์ด ์ž…๋ ฅ๋˜๋ฉด ์ฝ˜์†”์— ๊ฐ’์ด ์ž˜๋ชป๋๋‹ค๊ณ  ๊ฒฝ๊ณ ๊ฐ€ ๋œฌ๋‹ค.

// propTypes ์†Œ๋ฌธ์ž p โ—
Btn.propTypes = {
  text: PropTypes.string.isRequired, // PropTypes ๋Œ€๋ฌธ์ž p โ—
  // isRequired๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น prop์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ์ฝ˜์†”์— ๊ฒฝ๊ณ ์ฐฝ์„ ๋„์›Œ์ค€๋‹ค.
  fontSize: PropTypes.number,
};

const App = () => {
  const [value, setValue] = React.useState("Save Changes");
  const changeValue = () => setValue("Revert Changes");
  return (
    <div>
      <MemorizedBtn text={value} changeValue={changeValue} fontSize={18} />
      <MemorizedBtn text="Continue" />
    </div>
  );
};

(6) props์— ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ฌ์šฉ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์— ์˜ํ•ด ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
prop์˜ ๊ฐ’์ด undefined์ธ ๊ฒฝ์šฐ ์„ค์ •ํ•œ ๊ธฐ๋ณธ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.

const Btn = ({ text, changeValue, fontSize = 14 }) => {
  // ์ค‘๋žต
};

2. underdash ์ฝ”๋“œ ๋ฆฌ๋ทฐ ์ •๋ฆฌ(2)

1) ํ•จ์ˆ˜ ์žฌํ™œ์šฉ

(1) _.extend ํ•จ์ˆ˜

๐Ÿ”Ž ์ฒ˜์Œ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ (์ˆ˜์ • ์ „)

์œ ์‚ฌ ๋ฐฐ์—ด์ธ arguments๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค.
์ค‘์ฒฉ for ๋ฌธ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.

_.extend = function (obj) {
  for (let i = 1; i < arguments.length; i++) {
    for (key in arguments[i]) {
      obj[key] = arguments[i][key];
    }
  }
  return obj;
};

๐Ÿ”Ž _.each ํ•จ์ˆ˜๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๊ทธ๋ƒฅ for ๋ฐ˜๋ณต๋ฌธ ๋Œ€์‹  _.each ํ•จ์ˆ˜๋กœ ๋Œ€์ฒดํ•œ ๊ฒƒ๋ฟ์ด๋‹ค.
๋ญ”๊ฐ€ ๊ฐœ์„ ํ–ˆ๋‹ค๊ธฐ์—” ๋„ˆ๋ฌด ๋นˆ์•ฝํ•ด ๋ณด์—ฌ์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์žฌ์‹œ๋„ํ–ˆ๋‹ค.

_.extend = function (obj, ...args) {
  _.each(args, function (object) {
    _.each(object, function (value, key) {
      obj[key] = object[key];
    });
  });
}

๐Ÿ”Ž ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ _.each ํ•จ์ˆ˜, _.reduce ํ•จ์ˆ˜๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

  • ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•ด๋†“์€ _.each ํ•จ์ˆ˜์™€ _.reduce ํ•จ์ˆ˜
_.each = function (collection, iterator) {
  if (Array.isArray(collection)) {
    for (let i = 0; i < collection.length; i++) {
      iterator(collection[i], i, collection);
    }
  } else {
    for (key in collection) {
      if (collection.hasOwnProperty(key)) {
        iterator(collection[key], key, collection);
      }
    }
  }
};

_.reduce = function (collection, iterator, accumulator) {
  if (accumulator !== 0 && accumulator === undefined) {
    accumulator = collection[0];

    for (let i = 1; i < collection.length; i++) {
      accumulator = iterator(accumulator, collection[i]);
    }
  } else {
    for (key in collection) {
      if (collection.hasOwnProperty(key)) {
        accumulator = iterator(accumulator, collection[key]);
      }
    }
  }

  return accumulator;
};
  • _.extend ํ•จ์ˆ˜ ์ˆ˜์ •
_.extend = function (obj, ...args) {
  obj = _.reduce(
    args,
    function (acc, curr) {
      _.each(curr, function (value, key) {
        acc[key] = curr[key];
      });

      return acc;
    },
    obj
  );

  return obj;
};

ํ•จ์ˆ˜ ์žฌํ™œ์šฉ์„ ์—ฐ์Šต ์ค‘์ด๋ผ ์ผ๋‹จ ์ˆ˜์ •ํ•ด๋ณด๊ธด ํ–ˆ๋Š”๋ฐ ์ˆ˜์ • ์ „ ์ฝ”๋“œ๊ฐ€ ๋” ์ž˜ ์ฝํžˆ๋Š” ๊ฑฐ ๊ฐ™์€ ๊ฑด ๋‚ด๊ฐ€ ๋‚ด๊ณต์ด ๋ถ€์กฑํ•ด์„œ์ผ๊นŒ...

(2) _.defaults ํ•จ์ˆ˜

_.extend ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•œ ๋ฐฉ์‹๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ–ˆ๋‹ค.

๐Ÿ”Ž ์ฒ˜์Œ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ

 _.defaults = function (obj) {
    for (let i = 1; i < arguments.length; i++) {
      for (key in arguments[i]) {
        if (key in obj) {
          continue;
        }
        obj[key] = arguments[i][key];
      }
    }
    return obj;
  };

๐Ÿ”Ž ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ _.each ํ•จ์ˆ˜, _.reduce ํ•จ์ˆ˜๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

_.defaults = function (obj, ...args) {
  obj = _.reduce(
    args,
    function (acc, curr) {
      _.each(curr, function (value, key) {
        if (key in acc) return; // _.extend ํ•จ์ˆ˜์—์„œ ์ด ๋ถ€๋ถ„๋งŒ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•จ
        acc[key] = curr[key];
      });

      return acc;
    },
    obj
  );

  return obj;
};

2) ์žฌ๊ท€ ํ•จ์ˆ˜

์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ๋ช…์‹œ์ ์ธ return ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์žฌ๊ท€ํ•จ์ˆ˜์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ํฌ๊ฒŒ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

(1) ๋ช…์‹œ์ ์ธ return ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜

function deleteItem (arr) {
  if (arr.length === 0) {
    return;
  }
  
  arr.pop();
  const newArr = arr;
  deleteItem(newArr);
}

const hi = [1, 2, 3];

deleteItem(hi);
console.log(hi); // []

(2) ๋ช…์‹œ์ ์ธ return ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜

_.flatten ํ•จ์ˆ˜๋Š” ๋ช…์‹œ์ ์ธ return ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜์ด๋‹ค.
์ด ๊ฒฝ์šฐ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ ํ›„ ๊ทธ return ๊ฐ’์„ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.

_.flatten = function (nestedArray) {
  // Hint: Use Array.isArray to check if something is an array
  const flattenedArray = [];

  for (let i = 0; i < nestedArray.length; i++) {
    if (!Array.isArray(nestedArray[i])) {
      flattenedArray.push(nestedArray[i]);
    } else {
      const innerFlattedArray = _.flatten(nestedArray[i]); // ์žฌ๊ท€ ํ•จ์ˆ˜ return๊ฐ’์„ ๋ณ€์ˆ˜ innerFlattedArray์— ๋ฐ›์Œ
      flattenedArray.push(...innerFlattedArray); // ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ
    }
  }

  return flattenedArray;
};

โœจ ๋‚ด์ผ ํ•  ๊ฒƒ

  1. CREATE REACT APP

  2. underdash ์ฝ”๋“œ ์ตœ์ข… ์ •๋ฆฌ ํ›„ commit & push

profile
dev log

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด

Powered by GraphCDN, the GraphQL CDN