4 回答

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊
v
({id, title}) => ({id, title})
Object.assign
[p]
function pick(o, ...props) { return Object.assign({}, ...props.map(prop => ({[prop]: o[prop]})));}
configurable
function pick(o, ...props) { var has = p => o.propertyIsEnumerable(p), get = p => Object.getOwnPropertyDescriptor(o, p); return Object.defineProperties({}, Object.assign({}, ...props .filter(prop => has(prop)) .map(prop => ({prop: get(props)}))) );}

TA貢獻(xiàn)2021條經(jīng)驗 獲得超8個贊
pick
function pick(o, ...fields) { return fields.reduce((a, x) => { if(o.hasOwnProperty(x)) a[x] = o[x]; return a; }, {});}
var stuff = { name: 'Thing', color: 'blue', age: 17 };var picked = pick(stuff, 'name', 'age');

TA貢獻(xiàn)1784條經(jīng)驗 獲得超2個贊
orig
Array#reduce
initialValue
const orig = {
id: 123456789,
name: 'test',
description: '…',
url: 'https://…',
};
const filtered = ['id', 'name'].reduce((result, key) => { result[key] = orig[key]; return result; }, {});
console.log(filtered); // Object {id: 123456789, name: "test"}

TA貢獻(xiàn)1798條經(jīng)驗 獲得超7個贊
const pick = (O, ...K) => K.reduce((o, k) => (o[k]=O[k], o), {})
添加回答
舉報