shineYao

ScriptOJ-safeGet#99

const safeGet = (data, path) => {
  if(!path) return undefined;
  const pathArr = path.split('.');
  let result = data;
  let length = pathArr.length
  while(length--) {
    const iter = pathArr.shift();
    result = result[iter];
    if(result === undefined) return undefined;
  }
  return result
}

posted on 2017-09-03 22:58  shineYao  阅读(127)  评论(0编辑  收藏  举报

导航