Removing object properties with Lodash

后端 未结 8 1493
旧巷少年郎
旧巷少年郎 2021-02-02 05:30

I have to remove unwanted object properties that do not match my model. How can I achieve it with Lodash?

My model is:

var model = {
   fname: null,
   lna         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 05:45

    To select (or remove) object properties that satisfy a given condition deeply, you can use something like this:

    function pickByDeep(object, condition, arraysToo=false) {
      return _.transform(object, (acc, val, key) => {
        if (_.isPlainObject(val) || arraysToo && _.isArray(val)) {
          acc[key] = pickByDeep(val, condition, arraysToo);
        } else if (condition(val, key, object)) {
          acc[key] = val;
        }
      });
    }
    

    https://codepen.io/aercolino/pen/MWgjyjm

提交回复
热议问题