-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用ES6实现该方法 #32
Comments
坦白讲,这道题目是在一套笔试题中的最后一道,整套题目给了我一个小时完成,后来剩最后十分钟,还剩这一道题,当时也就多少有点慌了,还有就是,纯手写的话,有些思路打不开,也被他题目里特地强调的使用ES6吓到了。
刚刚简单实现了一下,要说有用ES6的就是箭头函数了。不知道还有哪些更好的方法。 |
这种情况下用
|
|
function foo (o, arr) {
const result = {}
Object.keys(o).filter(k => arr.includes(k)).map(s => { result[s] = o[s] })
return result
} |
大家都想得挺远...
|
我支持@zhangolve的版本 |
|
const foo = (obj, arr) => arr.reduce((ret, key) => Object.assign(ret, { [key]: obj[key] }), {}) 不需要 return 的写法 |
reduce 方法用的还是少啊~~~,平时真的很少用 |
|
let foo = (obj, arr) => JSON.parse(JSON.stringify(obj, arr)) |
@Wang-NQXB 你这方法会漏情况
这种情况你就会把 |
纯粹卖弄语法,娱乐一下: function foo(...args) {
const [o, arr] = args;
return arr.reduce((acc, curr) => ({
...acc,
[curr]: o[curr],
}), {});
}
// test
foo({a:1,b:2,c:3}, ['a', 'c']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//输出 {a:1,c:3}
The text was updated successfully, but these errors were encountered: