_.pick
Creates an object composed of the object properties predicate returns truthy for.
- var object = { 'a': 1, 'b': '2', 'c': 3 };
- // Underscore/Lodash
- var result = _.pick(object, ['a', 'c']);
- console.log(result)
- // output: {a: 1, c: 3}
- // Native
- const { a, c } = object;
- const result = { a, c};
- console.log(result);
- // output: {a: 1, c: 3}
- // for an array of this object --> array.map(({a, c}) => ({a, c}));
- // Native
- function pick(object, keys) {
- return keys.reduce((obj, key) => {
- if (object[key]) {
- obj[key] = object[key];
- }
- return obj;
- }, {});
- }
- var result = pick(object, ['a', 'c']);
- console.log(result)
- // output: {a: 1, c: 3}
Browser Support
38.0 ✔ | ✔ | 13.0 ✔ | 12.0 ✔ | 25.0 ✔ | 7.1 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .