迭代数组内的对象

时间:2018-06-19 23:28:43

标签: reactjs

如何从对象而不是"name"中获得{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 个答案:

答案 0 :(得分:1)

只需直接访问它即可。

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