4 回答
TA贡献1993条经验 获得超5个赞
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贡献2021条经验 获得超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贡献1784条经验 获得超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贡献1798条经验 获得超7个赞
const pick = (O, ...K) => K.reduce((o, k) => (o[k]=O[k], o), {})
添加回答
举报