0

I have a current Array of objects that looks like this:

this.subsidiaryOptions = [
  { label: FILTER_LABELS.topLevelOrganization, value: '0', selected: !this.includeOpportunities },
  { label: FILTER_LABELS.currentOrganization, value: '1', selected: !this.includeOpportunities }
];

now I have this new array like this one - i'm trying to add these fields as labels in the other array or adapt this array to be like the original one:

   this.engagementService.orgSubSidNames = 
   ['a', 'b', 'c', 'd', 'f', 'g']

I want to either make the current array include each member of this new arrays members in it's label field and then have the 'values' increase by 1 for each one, and add all of them have a 'selected' field.

Or add the fields into this new array, the array's size is not static either it will change.

I tried to use a push method with the labels like this:

    this.engagementService.orgSubSidNames.push.apply(this.engagementService.orgSubSidNames, this.subsidiaryOptions);

But couldnt get it to work, i was going to rap it in a for loop to increase the values but im getting an error on this push attempt.

expected result:

 Either array   = [
  { label: a, value: '0', selected: !this.includeOpportunities },
  { label: b, value: '1', selected: !this.includeOpportunities },
  { label: c, value: '2', selected: !this.includeOpportunities },
  { label: d, value: '3', selected: !this.includeOpportunities },
  { label: f, value: '4', selected: !this.includeOpportunities },
  { label: g, value: '5', selected: !this.includeOpportunities }
];

1 Answer 1

1

function namesAsOrgs({ includeOpportunities, orgSubSidNames }) {
  return orgSubSidNames.map((name, i) => {
    return {
      label: name,
      value: `${i}`,
      selected: !includeOpportunities
    }
  })
}

console.log(namesAsOrgs({
  includeOpportunities: true,
  orgSubSidNames: ['a', 'b', 'c', 'd', 'f', 'g']
}))

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

3 Comments

Thank you Callam, I am trying to understand why the Curly braces were used inside of the functions parameters? something to do with destructuring objects?
This is interpolation – it's very useful when you need to plug a string with variables. In this case, we are simply interpolating the index to make it a string, but this could also be done with '' + i
I can't put the result into a variable though so I can actually use it? like: var subSids = namesAsOrgs({ includeOpportunities: false, orgSubSidNames: this.engagementService.orgSubSidNames }) just returns as an empty array, it consoles fine but I need to use it?

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.