javascript return array除1个属性外的所有属性

Rod*_*her 0 javascript

我试图通过返回除1属性以外的所有值来使数组解析。我以为我可以利用传播,但这包括全部。既然我已经写出了整个对象,那么我担心将来会在数组中添加额外的属性。javascript中是否有任何功能可以让我做到这一点?任何提示将不胜感激!!!

//原始对象

0: {
       id: 1
    isActive: true
    isClassification: false
    isEditable: true
    isRequired: false
    isTeamType: false
    name: "Business Unit"
    termGroup: {id: 1, name: "Team Classifications", isTenantWide: false, termSets: Array(0)}
    termGroupId: 1
    termGroupName: "Team Classifications"
    terms: (3) [{…}, {…}, {…}]
}
Run Code Online (Sandbox Code Playgroud)

//我的解析代码

// args:
// @return      {array}     -  returns an array containing the termsets only.

getTermSets() {
console.log(this._termSetsWithChilden);

return this._termSetsWithChilden.map(termSet => {
    return {
        id: termSet.id,
        isActive: termSet.isActive,
        isClassification: termSet.isClassification,
        isEditable: termSet.isEditable,
        isRequired: termSet.isRequired,
        isTeamType: termSet.isTeamType,
        name: termSet.name,
        termGroupId: termSet.termGroupId,
        termGroupName: termSet.termGroupName,
        numberOfTerms: termSet.terms.length
    };
});
Run Code Online (Sandbox Code Playgroud)

}

//我的retun值

0: {
    id: 1
    isActive: true
    isClassification: false
    isEditable: true
    isRequired: false
    isTeamType: false
    name: "Business Unit"
    numberOfTerms: 3
    termGroupId: 1
    termGroupName: "Team Classifications"
}
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 5

如果我正确理解了您想要什么,则可以为此使用解构和剩余符号:

return this._termSetsWithChilden.map(({propertyYouDontWant, ...rest}) => rest);
Run Code Online (Sandbox Code Playgroud)

... propertyYouDontWant您想保留的财产在哪里?(如果有多个,请列出它们,例如({thisOne, thatOne, theOtherOne, ...rest})。)

现场示例:

return this._termSetsWithChilden.map(({propertyYouDontWant, ...rest}) => rest);
Run Code Online (Sandbox Code Playgroud)

如果您还想添加属性,则可以使用扩展符号来实现(但是会创建一个额外的不必要的对象):

return this._termSetsWithChilden.map(({terms, ...rest}) => ({...rest, numberOfTerms: terms.length}));
Run Code Online (Sandbox Code Playgroud)

...或仅将其添加到通过rest创建的对象中:

return this._termSetsWithChilden.map(({terms, ...rest}) => {
    rest.numberOfTerms = terms.length;
    return rest;
});
Run Code Online (Sandbox Code Playgroud)

现场示例:

const array = [
  {one: "uno", two: "dos", three: "tres"},
  {one: "uno", two: "due", three: "tre"},
  {one: "un", two: "deux", three: "trois"}
];
const result = array.map(({three, ...rest}) => rest);
console.log(result); // Only has `one` and `two`
Run Code Online (Sandbox Code Playgroud)