next.js 为什么if/else语句即使在条件满足时也要执行else部分

axkjgtzd  于 2023-10-18  发布在  其他
关注(0)|答案(2)|浏览(76)

所以我运行一个if/else语句来检查输入值是否与电影的标题相同,但是由于某种原因,即使标题和值相同,循环应该停止,它仍然继续运行并执行else语句上的指令。为什么会这样?.
这是我的代码目前的样子

import React from 'react'
import styles from '@/styles/movies.module.css'
import Image from 'next/image';

export default function index() {
    const movies = [
        {
            title: 'Batman: the dark knight',
            releaseDate: '27 April 2007',
            rating: 9.8,
            image: "/pic11.jpg"
        },
        {
            title: 'Scrubs',
            releaseDate: '18 April 2012',
            rating: 4.7,
            image: "/pic12.jpg"
        },
        {
            title: 'Ted',
            releaseDate: '16 April 2002',
            rating: 9,
            image: "/pic13.png"
        },
        {
            title: 'Transformers',
            releaseDate: '27 April 2007',
            rating: 8,
            image: "/pic14.jpg"
        },
        {
            title: 'Batman: the dark knight rises',
            releaseDate: '27 April 2007',
            rating: 9.2,
            image: "/pic15.jpg"
        },
    ];
    const func = (arg) => {
        for(let i = 0; i < movies.length; i++){
            if(arg === movies[i].title){
                console.log(movies[i].title)
                console.log(`The movie ${movies[i].title} has been found`)
            } else {
                console.log(`The movie ${arg} has not been found!`)
            }
        }
    }

  return (
    <div className={styles.fightContainer}>
        <div className={styles.topStuff}>
            <h1>Movie Fight!</h1>
            <input 
                type="text" 
                placeholder='eg Batman....' 
                name='arg'
                className={styles.inputation} 
                onKeyUp={(e) => func(e.target.value)}
            />
        </div>
        <div className={styles.interactionContainer}>
            {movies.map(movie => (
                <div className={styles.fightCard} key={movie.rating}>
                    <div className={styles.imageContainer}>
                        <Image src={movie.image} width={130} height={100} className={styles.image} alt="img" />
                    </div>
                    <div className={styles.textContainer}>
                        <p>{movie.title}</p>
                        <p>{movie.releaseDate}</p>
                        <p>{movie.rating}</p>
                    </div>
                    <button>More info</button>
                </div>
            ))}
        </div>
        <h1>hey</h1>
    </div>
  )
}

我能做错什么呢?.

cwdobuhd

cwdobuhd1#

这是因为你在满足条件后没有中断/退出循环,你可以在if条件内使用break

const func = (arg) => {
    for(let i = 0; i < movies.length; i++){
        if(arg === movies[i].title){
            console.log('The movie ${movies[i].title} has been found');
            break;
        } else {
            console.log('The movie ${arg} has not been found!');
        }
    }
}
jhiyze9q

jhiyze9q2#

你正在执行一个for循环:只要你不break它会继续执行.
在您的示例中:如果你写“Scrubs”,它将正确地进入你函数的第二个循环中的if语句,但是for循环不会停止,除非你打破它(这就是为什么你一直看到所有电影的控制台日志找不到)。
所以你可以在if中添加一个break,像这样:

const func = (arg) => {
    for(let i = 0; i < movies.length; i++){
        if(arg === movies[i].title){
            console.log(movies[i].title)
            console.log(`The movie ${movies[i].title} has been found`);
            break;
        } else {
            console.log(`The movie ${arg} has not been found!`)
        }
    }
}

相关问题