Function: only

only<T, K>(obj, keys): Pick<T, K>

返回对象的白名单属性

https://github.com/tj/node-only

Type parameters

Name Type Description
T extends object 对象类型
K extends string | number | symbol 键类型

Parameters

Name Type Description
obj T 源对象
keys string | K[] 白名单键,可以是空格分隔的字符串或数组

Returns

Pick<T, K>

  • 只包含白名单属性的新对象

Example

const obj = {
  name: 'tobi',
  last: 'holowaychuk',
  email: 'tobi@learnboost.com',
  _id: '12345'
};

const user = only(obj, 'name last email');
// => {
//      name: 'tobi',
//      last: 'holowaychuk',
//      email: 'tobi@learnboost.com'
//    }

Example

const obj = { a: 1, b: 2, c: 3 };
const result = only(obj, ['a', 'b']);
// => { a: 1, b: 2 }