Lodash _.get() Method

JJVoiture·2022년 8월 9일
0

lodash _.get은 object에서 원하는 값을 뽑아 낼때 유용한 method이다.

ex)

var obj = {
    "three": {
        "four": 4
    },
    "one.two": 1
}
console.log(_.get(obj, 'one.two')) //1

console.log(_.get(obj, 'three.four')) // 4

get method는 다음과 같이 정의되어 있다.

 function get(object, path, defaultValue) {
  var result = object == null ? undefined : baseGet(object, path);
  return result === undefined ? defaultValue : result;
}

function baseGet(object, path) {
  path = isKey(path, object) ? [path] : castPath(path);

  var index = 0,
      length = path.length;

  while (object != null && index < length) {
    object = object[toKey(path[index++])];
  }
  return (index && index == length) ? object : undefined;
}

Lodash _.get() method

profile
안녕하세요.

0개의 댓글