有没有更好的写作方法?

时间:2019-06-27 07:13:12

标签: javascript ecmascript-6

有没有更好的写作方法?我认为这太过剩了。

let {
        recruitmentType,
        positionName,
        positionType,
        labels,
        workAddressId,
        positionBrightPoint,
        positionDesc,
        department,
        jobNature,
        workYear,
        education,
        salary,
        isSchoolPosition
      } = this.data;
      windows.setStorage('saveReleaseJob', {
        recruitmentType,
        positionName,
        positionType,
        labels,
        workAddressId,
        positionBrightPoint,
        positionDesc,
        department,
        jobNature,
        workYear,
        education,
        salary,
        isSchoolPosition
      })

1 个答案:

答案 0 :(得分:0)

您可以使用一个函数来获取所需属性的子集。

const getOnly = (object, keys) => Object.assign({}, ...keys.map(k => ({ [k]: object[k] })));

var object = { a: 1, b: 2, c: 3, d: 4 },
    keys = ['a', 'b'],
    subset = getOnly(object, keys);

console.log(subset);

即将到来的Object.fromEntries

const getOnly = (object, keys) => Object.fromEntries(keys.map(k => [k, object[k]]));

var object = { a: 1, b: 2, c: 3, d: 4 },
    keys = ['a', 'b'],
    subset = getOnly(object, keys);

console.log(subset);