0

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?

9
  • 1
    I think map() will work. What is the expected output? Commented Mar 19, 2020 at 17:26
  • represent it where? In an HTML+CSS document? Commented Mar 19, 2020 at 17:27
  • What is the expected result? Commented Mar 19, 2020 at 17:27
  • @TKoL I would like to recreate this variable without showing properties that have the same values for each object. Commented Mar 19, 2020 at 17:29
  • So you want each object, but with only DisplayName, RealFieldName, and Name properties? Commented Mar 19, 2020 at 17:30

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

based on what he's said in the comments, I feel your answer would be what he's looking for if you just changed this bit: let result = fieldObjects.map(({DisplayName, RealFieldName, Name, ...rest}) => {DisplayName,RealFieldName,Name});. Those are the fields he seems to want to keep
@TKoL not sure either "without showing properties that have the same values for each object. " means those should be skipped but either way spread would be the way to go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.