javascript - How to deep pick in another pick with lodash? - Stack Overflow

Now i have code like this:var object = {a: 'a',b: 'b',c: {d: 'd'}}_.get

Now i have code like this:

var object = {
   a: 'a',
   b: 'b',
   c: {
       d: 'd'
   }
}
_.get(object).pick(['a', 'b']).value();

How to deep pick property 'd' like:

_.get(object).pick(['a', 'b', 'c.d']).value();

Now i have code like this:

var object = {
   a: 'a',
   b: 'b',
   c: {
       d: 'd'
   }
}
_.get(object).pick(['a', 'b']).value();

How to deep pick property 'd' like:

_.get(object).pick(['a', 'b', 'c.d']).value();
Share Improve this question asked Feb 25, 2019 at 20:21 Nikita IskorkinNikita Iskorkin 131 silver badge5 bronze badges 1
  • @OriDrori object with properties a, b and d – Nikita Iskorkin Commented Feb 25, 2019 at 20:36
Add a ment  | 

5 Answers 5

Reset to default 4

you can deep destructure without lodash :

var object = {
  a: 'a',
  b: 'b',
  c: {
      d: 'd'
  }
}

const { a, b, c :{ d }} = object;

console.log(a,b,d);

const obj = {a, b, d};

console.log(obj);

In case you insist in using Lodash, consider using the _.get() function:

_.get(object, 'c.d');

So, for the properties you want to get:

const selectedProps = {
  ..._.pick(object, ['a', 'b']),
  _.get(object, 'c.d')
}

You can create a flatPick() function. The function iterates the array of paths. and uses _.get() to get the value of the path, and _.set() to add the last part of the path as property on the result object:

function flatPick(object, paths) {
  const o = {};

  paths.forEach(path => _.set(
    o,
    _.last(path.split('.')),
    _.get(object, path)
  ));

  return o;
}

var object = {
  a: 'a',
  b: 'b',
  c: {
    d: 'd',
    e: {
      f: 'f'
    }
  }
};

var result = flatPick(object, ['a', 'b', 'c.d', 'c.e.f']);

console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.js"></script>

This can work for lodash:

function pickNested(object: Json, fields: string[]) {
    const shallowFields = fields.filter((field) => !field.includes('.'));
    const deepFields = fields.filter((field) => field.includes('.'));

    const initialValue = _.pick(object, shallowFields) as Json;

    return deepFields.reduce((output, field) => {
        const key = _.snakeCase(field);
        output[key] = _.get(object, field);
        return output;
    }, initialValue);
}

and:

const json = {
    id: '10',
    user: {
        email: '[email protected]',
    },
};

const newData = pickNested(json, ['id', 'user.email']);
console.log('newData ->', newData);
/*
{
    id: '10',
    user_email: '[email protected]',
};
*/

I am not very clear from your question if you want just the values to be extracted destructuring might be an option. But if you just want to pick specific properties eg. 'd', and keep the original json structure as it is, then there is a trick.

JSON.stringify(object, ['c','d'], 0)

Result:

'{"c":{"d":"d"}}'

Good thing is it works for array or objects.

x = {
 top1: 1, 
 top2: {
    level2: [
        { leaf1a: 'a', leaf1b: 'b'},
        {leaf2a:'2a', leaf2b: '2b'}
    ]
 }
}

With JSON.stringify(x, ['level2','top2', 'leaf2a'], 0)

Will result in below structure.

{
 "top2": {
  "level2": [
   {},
   {
    "leaf2a": "2a"
   },
   {}
  ]
 }
}

Its not perfect as you can see empty child elements, I only use this for analyzing deep nested structure with lots of attributes at each level, even used it sometimes for logging necessary items in a program.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745102311a4611348.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信