简体   繁体   English

Javascript-For循环无法在数组中正确找到值

[英]Javascript - For Loop not finding values in array correctly

I have an array with a list of customers. 我有一个包含客户列表的数组。 I am declaring a function that takes a customer name as a parameter. 我在声明一个以客户名称为参数的函数。 I want this function to loop through the array to find out whether or not the customer is in the array. 我希望此函数在数组中循环查找客户是否在数组中。

    var customers = [
        {fullName: 'Marshall Hathers',
         dob: '01/07/1970'},
        {fullName: 'Margaret May',
         dob: '01/07/1980'}
    ];

The function: 功能:

    function findCustomer(customer) {
       for(i=0; i<customers.length; i++) {
          var found; 
          if(customer === customers[i].fullName) {
              found = true; 
              console.log('Customer has been found');
              break;
          } else {
              found = false;
              console.log('Customer has not been found');
              break;
         }
    }

It works well the first time a customer is found, but it prints out incorrectly when trying to find the second customer. 第一次找到客户时,它运行良好,但在尝试找到第二个客户时,它打印不正确。

Can anyone please assist? 谁能帮忙吗?

So look at what you're actually saying in your loop. 因此,请看一下您实际上在循环中所说的话。 The loop body will run for each customer. 循环主体将为每个客户运行。 So you're saying 所以你是说

For the first customer in the array
    if this is my customer
        print "found" and stop looping
    otherwise
        print "not found" and stop looping

Does that look right to you? 您觉得合适吗? Does looking at the first record alone really tell you that the customer isn't found? 仅查看第一条记录是否真的告诉您找不到客户?

And note that since all possibilities end with "and stop looping" the 2nd record is never examined. 请注意,由于所有可能性都以“并停止循环”结尾,因此永远不会检查第二条记录。 The whole point of a loop is that in at least some condition, you don't stop looping, right? 循环的全部意义在于,至少在某些情况下,您不会停止循环,对吗? So that you would then see the step repeated for the 2nd, and so on... 这样您便可以看到第二步的重复步骤,依此类推...

Omit the else part and break the for loop if found. 忽略else部分,如果找到则中断for循环。

function findCustomer(customer) {
    var found, i;
    for (i = 0; i < customers.length; i++) {
        if (customer === customers[i].fullName) {
            found = true;
            console.log('Customer has been found');
            break;
        }
    }
    if (!found) {
        console.log('Customer has not been found');
    }
}

Use the Array.some prototype function to find the element 使用Array.some原型函数查找元素

function findCustomer(customer) {
    var found = customers.some(function(item) {return item.fullName == customer;});
    console.log(found ? 'Customer has been found': 'Customer has not been found');
}

You are exiting the loop when your script reach to a break 当脚本中断时,您将退出循环

So if you look for the second customer, you will enter in the "else". 因此,如果您寻找第二个客户,则将输入“ else”。 And there you have got a break that exit from the loop, so you will never get the console.log 而且您已经从循环中退出,因此您将永远无法获得console.log

I would change the code like this (edited as suggested in comment) 我会像这样更改代码(按注释中的建议进行编辑)

 var customers = [{ fullName: 'Marshall Hathers', dob: '01/07/1970' }, { fullName: 'Margaret May', dob: '01/07/1980' }]; function findCustomer(customer) { for (i = 0; i < customers.length; i++) { if (customer === customers[i].fullName) { console.log('Customer has been found'); return true; } } console.log('Customer has not been found'); return false; } findCustomer('Marshall Haters'); 

Just remove the break; 删除中断即可; statement from the else block; else块中的语句; Here I rewritten the function for you. 在这里,我为您重写了该功能。

function findCustomer(customer) {
var found = false; 
       for(i=0; i<customers.length; i++) {

          if(customer === customers[i].fullName) {
              found = true;              
              break;
          } else {
              found = false;
         }
    }

    if(found){
        console.log('Customer has been found');
    }else{
         console.log('Customer has not been found');
    }
}

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

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