面试手写第四期. lodash相关方法

一. lodash.merge方法

const getRawType = (val) => {
    
    
  return Object.prototype.toString.call(val).slice(8, -1)
}

const isPlainObjectOrArray = (val) => {
    
    
  return isPlainObject(val) || Array.isArray(val)
}

const isPlainObject = (val) => {
    
    
  return getRawType(val) === 'Object'
}

const merge = (object, ...sources) => {
    
    
  for(const source of sources) {
    
    
    for(const key in source) {
    
    
      if(source[key] === undefined && key in object) {
    
    
        continue
      }
      if(isPlainObjectOrArray(source[key])) {
    
    
        if(getRawType(object[key] === getRawType(source[key]))) {
    
    
          if(isPlainObject(object[key])) {
    
    
            merge(object[key], source[key])
          } else {
    
    
            object[key] = object[key].concat(source[key])
          }
        } else {
    
    
          object[key] = source[key]
        }
      } else {
    
    
        object[key] = source[key]
      }
    }
  }
}
// merge array
var object = {
    
    
  a: [{
    
     b: 2 }, {
    
     d: 4 }],
};
merge(object, {
    
     a: [{
    
     c: 3 }, {
    
     e: 5 }] });
console.log(object);

二. lodash.keyBy方法

// Output: {
    
    
//   "1": {
    
    
//     "id": 1,
//     "name": "山月"
//   },
//   "2": {
    
    
//     "id": 2,
//     "name": "shanyue"
//   }
// }
keyBy(
  [
    {
    
     id: 1, name: "山月" },
    {
    
     id: 2, name: "shanyue" },
  ],
  (x) => x.id,
);

function keyBy(list, by) {
    
    
  return list.reduce((acc, x) => {
    
    
    acc[by(x)] = x;
    return acc;
  }, {
    
    });
}

三. lodash.groupBy

groupBy(
  [
    {
    
     id: 1, name: "山月", sex: "male" },
    {
    
     id: 2, name: "张三", sex: "female" },
    {
    
     id: 3, name: "李四", sex: "female" },
  ],
  (x) => x.sex,
);

function groupBy(collection, by) {
    
    
  return collection.reduce((acc, x) => {
    
    
    if (acc[by(x)]) {
    
    
      acc[by(x)].push(x);
    } else {
    
    
      acc[by(x)] = [x];
    }
    return acc;
  }, {
    
    });
}

四. lodash.once

const f = (x) => x;
 
const onceF = once(f);
 
//=> 3
onceF(3);
//=> 3
onceF(4);

function once(f) {
    
    
  let result;
  let revoked = false;
  return (...args) => {
    
    
    if (revoked) return result;
    const r = f(...args);
    revoked = true;
    result = r;
    return r;
  };
}

五. 如何实现一个 omit/omitBy 函数

const object = {
    
    
  a: 3,
  b: 4,
  c: 5,
};
 
//=> { c: 5 }
omit(object, ["a", "b"]);
 
// omit by value
//=> { b:4, c: 5 }
omitBy(object, (value) => value === 3);

function omit(source, keys) {
    
    
  return Object.keys(source).reduce((target, nowKey) => {
    
    
    if (!keys.includes(nowKey)) target[nowKey] = source[nowKey];
    return target;
  }, {
    
    });
}
 
function omitBy(source, filiterFn) {
    
    
  return Object.keys(source).reduce((target, nowKey) => {
    
    
    if (!filiterFn(source[nowKey])) target[nowKey] = source[nowKey];
    return target;
  }, {
    
    });
}

六. lodash.get方法

function lodashGet(object, path, defaultValue) {
    
    
  let obj = object
  if(typeof path === 'string') {
    
    
    const reg = /[^\[\]""''.]+/g
    path = path.match(reg)
  }
  for(let key of path) {
    
    
    if(!obj) {
    
    
      return defaultValue
    }
    obj = obj[key]
  }
  return obj
}
const object = {
    
     a: [{
    
     b: {
    
     c: 3 } }] };
console.log(get(object, 'a[0]["b"]["c"]'));
const object = {
    
     a: [{
    
     b: {
    
     c: 3 } }] };
//=> 3
get(object, "a[0].b.c");
//=> 3
get(object, 'a[0]["b"]["c"]');
//=> 10086
get(object, "a[100].b.c", 10086);

猜你喜欢

转载自blog.csdn.net/qq_63299825/article/details/135954776