0

How do I get "name" from objects instead of {item}?

class Abc extends Component {
    state = {
        tags: [
            {
                "id": 1
                "name": "goal"
            },
            {
                "id": 2
                "name": "kick"
            }
        ]
    }

    render() {
        return (
            .................................
            <div>
                {this.state.tags.map((item, index) => (
                    <span key={index}>{item}</span>
                ))}
            </div>
            ...................................
        );
    }
}

1 Answer 1

1

Just access it directly.

class Abc extends Component {
    state = {
        tags: [
            {
                "id": 1
                "name": "goal"
            },
            {
                "id": 2
                "name": "kick"
            }
        ]
    }

    render() {
        return (
            .................................
            <div>
                {this.state.tags.map((item, index) => (
                    <span key={item.id}>{item.name}</span>
                ))}
            </div>
            ...................................
        );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You should also use the object's id property as the key, rather than the index (assuming the id property is unique)
Edited to implement suggestion from @Jayce444

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.