(Javascript)Console.log输出对象的值,并在下一行中添加未定义的值

(Javascript)Console.log输出对象的值,并在下一行中添加未定义的值,javascript,undefined,console.log,Javascript,Undefined,Console.log,当我调用函数outputCustomerbyId(customer_id)时,它以一种非常奇怪的方式输出getAddressbyId(address_id)函数的值:在console.log语句上方的一行,并在其位置添加未定义的值。我在我的其他函数中也一直在努力解决这个问题,但是如果我能解决这个问题,其他函数的处理过程也是一样的。你们能帮帮我吗 输出: Customer 71: Martin Scott(mdog33@gmail.com) 287 Brant St. Apt 4A Waterdo

当我调用函数outputCustomerbyId(customer_id)时,它以一种非常奇怪的方式输出getAddressbyId(address_id)函数的值:在console.log语句上方的一行,并在其位置添加未定义的值。我在我的其他函数中也一直在努力解决这个问题,但是如果我能解决这个问题,其他函数的处理过程也是一样的。你们能帮帮我吗

输出:

Customer 71: Martin Scott(mdog33@gmail.com)
287 Brant St. Apt 4A Waterdown, ON. R93G3P
Home Address : undefined
Joined: Sat Feb 18 2017 19:11:42 GMT-0500 (Eastern Standard Time)
我有一个数据对象:

var allData = [
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
{type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
{type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "dbennett@gmail.com", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "jstevens22@hotmail.com", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "spym99@hotmail.com", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "steven2231@hotmail.com", address_id: 1836, add_date: null}},
{type:"customer", data:{customer_id: 71, store_id:614, first_name: "Martin", last_name: "Scott", email: "mdog33@gmail.com", address_id: 2727, add_date: null}},
{type:"customer", data:{customer_id: 24, store_id:614, first_name: "Jonathan", last_name: "Pym", email: "jjpym@yahoo.ca", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 36, store_id:193, first_name: "Kaitlyn", last_name: "Adams", email: "katy38@hotmail.com", address_id: 5464, add_date: null}},
{type:"customer", data:{customer_id: 73, store_id:297, first_name: "Melissa", last_name: "Bennett", email: "mbennett@gmail.com", address_id: 4536, add_date: null}},         
{type:"address", data:{address_id: 1023, address: "2895 Yonge St.", city:"Toronto", province:"ON", postal_code:"L4C02G"}},
{type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}},
{type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}},
{type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}},
{type:"address", data:{address_id: 2473, address: "391 Baker St. Apt 231", city: "Mississauga", province: "ON", postal_code: "M4T8S3"}},
{type:"address", data:{address_id: 1611, address: "183 City Ct.", city: "Hamilton", province: "ON", postal_code: "J3T9V2"}},
{type:"address", data:{address_id: 1836, address: "67 Rhymer Ave.", city: "Stouffville", province: "ON", postal_code: "L3C8H4"}},
{type:"address", data:{address_id: 2727, address: "287 Brant St. Apt 4A", city: "Waterdown", province: "ON", postal_code: "R93G3P"}},
{type:"address", data:{address_id: 5464, address: "11 New St. Apt 2B", city: "Brampton", province: "ON", postal_code: "L694R7"}},
];
这是我的CustomerDb对象:

         var CustomerDB = {

         customers: [],
         addresses: [],
         stores: [],
         insertData: function(someData){
             for(var i=0;i<someData.length;i++){
               if(someData[i].type=="store"){
                   this.addStore(someData[i].data);
               }else if(someData[i].type == "customer"){
                   this.addCustomer(someData[i].data);
               }else if(someData[i].type == "address"){
                   this.addAddress(someData[i].data);
               }
                console.log(someData[i].type);
             }
         },
         addStore: function(storeObj){
             this.stores.push(storeObj);
         },
         addCustomer : function(customerObj){
             customerObj.add_date= new Date() ;    
             this.customers.push(customerObj);
        },
         addAddress : function(addressObj){
             this.addresses.push(addressObj);  
         },
         outputCustomerById: function (customer_id){
             for(var i=0;i<this.customers.length;i++){
                if(this.customers[i].customer_id===customer_id ){
                  console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
                  console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");
                  console.log("Joined: "+this.customers[i].add_date+"\n\n");
                }
            }
         },

getAddressById: function(address_id){
    for(var j=0;j<this.addresses.length;j++){
        if(this.addresses[j].address_id===address_id){
            console.log(this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code);

        }
    }

 },

         outputAllCustomers: function( ){

            console.log("All Customers\n\n");
            for(var i=0;i<this.customers.length;i++){

                 console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
                 console.log("Home Address: "+this.addresses[i].address+" "+this.addresses[i].city+", "+this.addresses[i].province+". "+this.addresses[i].postal_code+"\n");
                 console.log("Joined: "+this.customers[i].add_date+"\n\n");
            }
         },
    };
您的
getAddressById()
函数中有一个
console.log()
,它在函数返回之前运行,因此该地址显示在控制台中自己的行上

函数没有显式返回值,因此它返回
未定义的
,这就是为什么:

console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");
…就像这样说:

console.log("Home Address : "+ undefined +"\n");
更改函数以返回有问题的值,而不是记录该值:

getAddressById: function(address_id){
    for(var j=0;j<this.addresses.length;j++){
        if(this.addresses[j].address_id===address_id){
            return this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code;    
        }
    }
    return ""; // address not found, so return a default value
 }

谢谢你,老兄,我的大脑在这件事上冻结了,我专注于另一件事,忘记了最明显的部分
getAddressById: function(address_id){
    for(var j=0;j<this.addresses.length;j++){
        if(this.addresses[j].address_id===address_id){
            return this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code;    
        }
    }
    return ""; // address not found, so return a default value
 }