简体   繁体   English

是否存在用于在数组中查找json对象的当前索引的函数?

[英]Is there a function for finding the current index of an json object in an array?

At onClick i want to memorize the name in localStorage but i can't figure out how to send as parametr the correct object from array 在onClick上,我想记住localStorage中的名称,但是我不知道如何从数组中发送正确的对象作为参数

myFunction(value){
    console.log(value)
    localStorage.setItem('name', value)
}

render() {
    var { isLoaded, items} = this.state;

    if (!isLoaded){
        return <div>Loading...</div>
    }else {
        return (
            <Container>

                <Jumbotron>
                    <h1 align="center">The Clinicum</h1>
                </Jumbotron>



                <Row>
                    {items.map(row =>(
                        <Col  key={row.iddoctor}>
                            <Image src= {require(`./photos/${row.photoLink}.jpg`)} 
                             roundedCircle onClick={() 
                             => this.myFunction(this.state.items.name)} />
                        </Col>
                    ))}
                </Row>

if i use this.state.items the console.log return the hole json correctly so everything else is working just fine, maybe a stupid question but i am pretty noob in js and i can't find the answer 如果我使用this.state.items console.log正确返回hole json,那么其他一切工作都很好,也许是一个愚蠢的问题,但是我在js中相当菜鸟,我找不到答案

Do it like this 像这样做

myFunction(value){
    console.log(value)
    localStorage.setItem('name', value)
}

render() {
    var { isLoaded, items} = this.state;

    if (!isLoaded){
        return <div>Loading...</div>
    }else {
        return (
            <Container>

                <Jumbotron>
                    <h1 align="center">The Clinicum</h1>
                </Jumbotron>



                <Row>
                    {items.map(row =>(
                        <Col  key={row.iddoctor}>
                            <Image src= {require(`./photos/${row.photoLink}.jpg`)} 
                             roundedCircle onClick={() 
                             => this.myFunction(row.name)} />
                        </Col>
                    ))}
                </Row>

If I understand correctly, you're wanting to save the name of the item that corresponds to the clicked item, to localStorage . 如果我理解正确,那么您想要将与单击的项目相对应的项目name保存到localStorage

One way to achieve that would be to modify your render code as follows: 一种实现方法是如下修改渲染代码:

<Row>
    {items.map((row) =>(
        <Col  key={row.iddoctor}>
            <Image src= {require(`./photos/${row.photoLink}.jpg`)} 
                roundedCircle onClick={() => this.myFunction(row.name)} />
        </Col>
    ))}
</Row>

You can also access the index of the current row item being rendered via the second argument passed to the mapping callback: 您还可以通过传递给映射回调的第二个参数访问当前呈现的row项目的索引:

<Row>
    {items.map((row, rowIndex) =>(
        <Col  key={row.iddoctor}>
            <Image src= {require(`./photos/${row.photoLink}.jpg`)} 
                roundedCircle onClick={() => this.myFunction(this.state.items[rowIndex].name)} />
        </Col>
    ))}
</Row>

Hope these pointers help! 希望这些指针对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM