cts
cts

Reputation: 1084

Using map function to create React components

I want to map over values and return an instance of my <FirstRepeatAttributeLabelAssistant /> for every label (basically to render an label above each input, number of inputs can vary).

I've started off with this:

{object.attributeCollection.questions.map((question) => (
   <FirstRepeatAttributeLabelAssistant />
))}

The output of the map above is like so:

[StringAttributeModel, MemoAttributeModel, LabelAttributeModel, MemoAttributeModel, StringAttributeModel, StringAttributeModel] I only care about the "StringAttributeModel" as each of those 3 include 3 different labels (the part I care about). Their structure is like so:

enter image description here

How can I update my logic to ensure all of the potential labels are covered and a <FirstRepeatAttributeLabelAssistant /> component is rendered for each of them?

Upvotes: 0

Views: 120

Answers (1)

windmaomao
windmaomao

Reputation: 7680

Suppose all your elements are a component, then you can simply do.

{object.attributeCollection.questions.map(Comp => {
  ...
  return <Comp label="..." />
})}

You can fill whatever your custom logic in the ... area.

Upvotes: 1

Related Questions