#99 safeGet


  • 0

    这题有问题,测试用例和结果对不上。


  • 0

    new Function()执行字符串版本

    const safeGet = (data, path) => {
      try {
        return new Function(...Object.keys(data), `return ` + path)(...Object.values(data));
      } catch (e) {
        return undefined;
      }
    }
    

  • 0

    const safeGet = (data, path) => {
      let arr = path.split('.'), cou
      while (true) {
        cou = arr.shift()
        if (cou === undefined) break
        data = 'object' === typeof data ? Reflect.get(data, cou) : undefined
        if (data === undefined) break
      }
      return data
    }

  • 0

        	const safeGet = (data, path) => {	
    		  /* TODO */
    		  let arr = path.split('.');
    		  for(let i=0;i<arr.length;i++){
    		      	data = data[arr[i]]
    		      	if(!data) return undefined;	            
    		   }	
    		   return data;
    		}
    

  • 0

    const safeGet = (data, path) => {
      let temp = data
      path.split('.').forEach(item => {
        if (temp) {
          temp = temp[item]
        }
      })
      return temp
    }
    

  • 0

    @ScriptOJ 什么jb玩意?看个参考答案设置重重障碍,这不是歪曲了使用的真实目的吗??


  • 0

    @ScriptOJ

    const safeGet = (data, path) => {
      /* TODO */
      const pathArrParent = path.split('.').slice(0, -1)
      const pathLastChild = path.split('.').slice(-1)[0]
      const parent = pathArrParent.reduce((pre, cur) => {
        return typeof pre[cur] === 'object' ? pre[cur] : {}
      }, data)
      return parent[pathLastChild]
    }
    

  • 0

    更优解

         const safeGet = (data, path) => {
            return path.split('.').reduce((pre, cur) => {
              if (pre) {
                return pre[cur];
              }
            }, data);
          };
    
    

  • 0

    const safeGet = (data, path) => {
      const ary = path.split('.');
      for (let i = 0; i < ary.length; i++) {
        const item = data[ary[i]];
        if (!item) return undefined;
        if (item && i !== ary.length - 1) {
          data = item;
          continue;
        } else 
          return item;
      }
    }
    

  • 0

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

  • 0

    const safeGet = (data, path) => {
      let pathList = path.split('.');
      let r = { ...data };
      let tar = pathList.every(el => {
        r = r && r[el];
        return !!r;
      });
      return tar ? r : undefined;
    };
    

  • 0

    输入 {"q":{"w":{"e":{"r":{"t":{"y":{"u":{"i":{"o":{"p":{"a":{"s":{"d":{"f":"ScriptOJ"}}}}}}}}}}}}}},路径:q.w.e.r,应该输出 {"t":{"y":{"u":{"i":{"o":{"p":{"a":{"s":{"d":{"f":"ScriptOJ"}}}}}}}}}},而你输出的是 undefined

    const safeGet = (data, path) => {
    let pathKey = path.split('.')
    let keyWord = data
    let keyNum = 0
    while(keyNum < pathKey.length) {
    keyWord = keyWord[pathKey[keyNum]]
    keyNum++
    if(!keyWord) {
    break;}
    }
    console.log(keyWord)
    }

    我定义的这个safeGet按照上面的测试题目,我在调试器里验证获得就是正确答案啊,都不是输出undefined的啊!
    怎么会说我的答案有误呢???


  • 0

    const safeGet = (data, path) => {
    /* TODO */
    path = path && path.split(".") || []
    let d;
    while(path.length && (d = data[path.shift()])) {
    data = d;
    }
    return d;
    }


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待