#99 safeGet


  • 0

    @coco ...........


  • 0

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

  • 0

    写的没有那么酷炫,能用
    const safeGet = (data, path) => {
    if(!path) return undefined;
    const props = path.split('.');
    const len = props.length;
    for(let i = 0; i < len; i++){
    if(data[props[i]]){
    data = data[props[i]];
    }else{
    return undefined;
    }
    }
    return data;
    }


  • 0

    const safeGet = (obj, str) => {
    /* TODO */
    if (str.indexOf('.') >= 0) {
    let arr = str.split('.');
    let myobj = obj;
    for (let i = 0; i < arr.length; i++) {
    if (myobj[arr[i]]) {
    myobj = myobj[arr[i]];
    if (i == (arr.length - 1)) {
    return myobj;
    }
    } else {
    return undefined;
    }
    }
    } else {
    return obj[str];
    }
    }


  • 0

    const safeGet = (data, path) => {
    var arr = path.split('.');
    for(i of arr) {
    if(data[i]) {
    data = data[i];
    }
    else {
    data = undefined;
    break;
    }
    }
    return data;
    }


  • 0

    const safeGet = (data, path, u) => {
    /* TODO */
    try{
    return eval('data.'.concat(path))
    }catch(e){
    return u
    }
    }


  • 0

    输入 {"q":{"w":{"e":{"r":{"t":{"y":{"u":{"i":{"o":{"p":{"a":{"s":{"d":{"f":{"g":{"h":{"j":"ScriptOJ"}}}}}}}}}}}}}}}}}, 路径:q.w.e.r,应该输出 undefined,而你的代码出现异常
    q.w.e.r 不是应该是{t:{y...}....}吗?到达t这一层。为什么说要输出undefined?


  • 0

    const safeGet = (data, path) => {
      path = path.split('.');
      
      for(let curr of path) {
        if(data[curr] === undefined) return undefined;
        data = data[curr];
      }
      return data;
    }

  • 0

    const safeGet = (data, path) => {
      /* TODO */
      let arr = path.split(".");
      let r = data;
      for(var i=0;i<arr.length;i++){
        try{
          r = r[arr[i]];
        }catch(e){
          return undefined
        }
      }
      return r;
    }
    

  • 0

    const safeGet = (data, path) => {
      /* TODO */
      path.split('.').forEach(item => {
        try{
          data = data[item];
        }catch(e){}
      })
      return data
    }
    

  • 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;
    }

登录后回复
 

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