cfoster5
cfoster5

Reputation: 1826

Create object array from same structure

I have the following object array that maintains the same structure:

var fieldObjects = [{
  AllowGridEditing: 'FALSE',
  DisplayName: 'Submit',
  RealFieldName: 'SubmitField',
  Name: 'SubmitField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Hours Up',
  RealFieldName: 'HoursUpField',
  Name: 'HoursUpField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Personal Hours',
  RealFieldName: 'PersonalHoursField',
  Name: 'PersonalHoursField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Hours Down',
  RealFieldName: 'HoursDownField',
  Name: 'HoursDownField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}]

I would like to represent this in a more readable manner. All properties should remain the same except DisplayName, RealFieldName, and Name. How can I recreate this array without declaring the entire structure for each object?

Upvotes: 0

Views: 43

Answers (1)

mickl
mickl

Reputation: 49945

You can use spread operator:

let result = fieldObjects.map(({DisplayName, RealFieldName, Name, ...rest}) => rest);

var fieldObjects = [{
  AllowGridEditing: 'FALSE',
  DisplayName: 'Submit',
  RealFieldName: 'SubmitField',
  Name: 'SubmitField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Hours Up',
  RealFieldName: 'HoursUpField',
  Name: 'HoursUpField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Personal Hours',
  RealFieldName: 'PersonalHoursField',
  Name: 'PersonalHoursField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}, {
  AllowGridEditing: 'FALSE',
  DisplayName: 'Hours Down',
  RealFieldName: 'HoursDownField',
  Name: 'HoursDownField',
  FieldType: 'Text',
  Type: 'Text',
  Filterable: 'FALSE',
  Sortable: 'FALSE',
  ReadOnly: 'TRUE',
}]

let result = fieldObjects.map(({DisplayName, RealFieldName, Name, ...rest}) => rest);
console.log(result);

Upvotes: 1

Related Questions