简体   繁体   中英

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.

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

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". And there you have got a break that exit from the loop, so you will never get the 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; 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');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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