无法突破 some() 数组方法

Can't break out of some() array method

我得到了一个附加到按钮的点击事件,用于执行检查某个元素是否符合某个条件的搜索。在下面的代码片段中,有一个 some() 数组方法检查 'entriesFound' 数组中是否有符合特定条件的元素。然而,一切正常,直到 else if(el.name !== name.value) 条件。警报框显示,但我需要点击警报框中的“确定”按钮的次数与 entriesFound 数组中的元素一样多。

import { persons } from './main.js';

export let entriesFound = []

export const searchBtn = document.querySelector('.search').addEventListener('click' , function() {
     let name = document.querySelector('.searchInput')

        if(name.value === "") {
            alert('No search query!')
            return;
        }

        entriesFound.some( el => {
            if(el.name === name.value){
                name.value = ""
                alert("You\'ve already found what you are looking for!")
                el.remove();

            // from here things go wrong

            }else if(el.name !== name.value){
                alert("No data found!")
                return;
            }
        })

        persons.some( el => {
            if(el.name === name.value) {
                addItem(el)
                entriesFound.push(el);
            }
        })
    name.value = ""
    localStorage.setItem('entriesFound', JSON.stringify(entriesFound))
}) 

你应该使用 some 的 return 值,你可以利用 find:

import { persons } from "./main.js";

export let entriesFound = [];

export const searchBtn = document
  .querySelector(".search")
  .addEventListener("click", function () {
    let name = document.querySelector(".searchInput");

    if (name.value === "") {
      alert("No search query!");
      return;
    }

    const entryExists = entriesFound.some((el) => el.name === name.value);

    if (entryExists) {
      name.value = "";
      alert("You've already found what you are looking for!");
      el.remove();

      // from here things go wrong
    } else {
      alert("No data found!");
      return;
    }

    const item = persons.find(el.name === name.value);
    if (item !== null) {
      addItem(item);
      entriesFound.push(item);
    }

    name.value = "";
    localStorage.setItem("entriesFound", JSON.stringify(entriesFound));
  });

谢谢大家。我结合了你的解决方案。我使用了常规的 for 循环和 every() 数组方法。

import { persons } from "./main.js";

export let entriesFound = [];

export const searchBtn =  
document.querySelector('.search').addEventListener('click' , () => {

    let name = document.querySelector('.searchInput')

    if(name.value === "") {
        alert('No search query!')
        return;
    }

    if(entriesFound.length > 0){
        for(let el of entriesFound){
            if(el.name === name.value) {
                alert('You have found that one already!')
                name.value = ""
                return;
            }
        }
    }

    if(persons.length > 0 ){
        for(let el of persons) {
            if(el.name === name.value) {
                addItem(el)
                entriesFound.push(el)
            }
        }
    }else if(persons.length === 0 ){
        alert('No data!')
        name.value = ""
        return;
    }
    
    
    let noMatch = persons.every( el => el.name !== name.value)
    console.log(noMatch)
    if(noMatch === true){
        alert('No match!');
        name.value = ""
        return;
    }

        name.value = ""
        localStorage.setItem('entriesFound', JSON.stringify(entriesFound))
});