2

If I have a static array of 4 objects and I would like to pass several data items from specifically from the third object in this array, how can this be done?

Here is an example:

const ENTRIES = [
{
name: "John"
color: "#fffff"
food: "Pasta"
},
{
name: "Ann"
color: "#f3f3f3"
food: "Salad"
},
{
name: "Mark"
color: "#000000"
food: "Sushi"
},
{
name: "Alice"
color: "#0f3cfs"
food: "Burger"
},
]

export default class SpecificItem extends Component {
  render() {
    return (
      <SafeAreaView>
        <View>
            <Card
              name={ENTRIES.name}
              color={ENTRIES.color}
              food={ENTRIES.food}
            />
        </View>
      </SafeAreaView>
    );
  }
}

If we take the following example, how would I specifically pass props for the 3rd object item with the details name: "Mark", color: "#000000", and food: "Sushi" into the component?

2 Answers 2

2

ENTRIES is array holding objects, for each object you should map through it, and for specific index filter for it -

  <View>
   {ENTRIES.filter((entry, index) => index == 2 && (
            <Card
              name={entry.name}
              color={entry.color}
              food={entry.food}
            /> 
   ))}
  </View>

will give you only the third object in the array.

of course, you can set the checker as something more dynamic like

index == this.state.someIndexState &&

and declare someIndexState state

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

Comments

1

Your can use map over each object

<View>
   {ENTRIES.map((entry, index) => (
            <Card
              name={entry.name}
              color={entry.color}
              food={entry.food}
            /> 
   ))}

Comments

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.