Skip to content

Instantly share code, notes, and snippets.

@monsterooo
Created September 10, 2018 03:33
Show Gist options
  • Save monsterooo/d61e5974acbb41581b2536a73a7bdd70 to your computer and use it in GitHub Desktop.
Save monsterooo/d61e5974acbb41581b2536a73a7bdd70 to your computer and use it in GitHub Desktop.
pick对象key/value
const pick = (obj, keys) => {
return keys.filter(key => obj.hasOwnProperty(key)).reduce((preValue, key) => {
preValue[key] = obj[key];
return preValue;
}, {});
};
// var o = {
// a: 1,
// b: 2,
// c: 3
// };
// const result = pick(o, ["a", "c"]);
// console.log("测试pick > ", o, result);
// 测试pick > {a: 1, b: 2, c: 3} > {a: 1, c: 3}
export default pick;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment