(Javascript)Console.log输出对象的值,在下一行添加undefined

(Javascript) Console.log outputs the value of the object and adds undefined in the next row

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

输出:

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");
            }
         },
    };

调用函数:

CustomerDB.insertData(allData);
CustomerDB.outputCustomerById(71);

您的 getAddressById() 函数内部有一个 console.log(),它在函数 return 之前 运行s,所以这就是为什么地址出现在它自己的行中控制台。

并且该函数没有明确 return 一个值,所以它 return 是 undefined,这就是为什么:

console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");

...就像这样说:

console.log("Home Address : "+ undefined +"\n");

将函数更改为 return 有问题的值而不是记录它:

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
 }

(注:我在最后加的return语句只有在找不到地址的情况下才会到达。我不知道你想做什么,所以我'我们刚刚 return 编辑了一个空字符串。您可能想要 return nullundefined 或抛出错误。)

展开并运行以下代码段以查看它是否正常工作。我在你的代码中唯一改变的是我上面提到的加上注释掉插入函数中的 console.log()

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"}},
];

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){
            return this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code;

        }
    }
    return "";
 },

         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");
            }
         },
    };
    
CustomerDB.insertData(allData);
CustomerDB.outputCustomerById(71);