2015-10-13 72 views
0

我试图循环和添加基于尊重的数据类型。 我假设ID和类型将被放在下面的代码中,但我得到[对象窗口]来代替ID。 这里是我的代码:javascript [对象窗口] concat字符串

//List array FriendID and Type 
var friendArray=[]; 

$('.active').each(function(i, obj) { 
    friendID = $(this).siblings('span:first').data('type'); 
    console.log(friendID); //#1 
    friendType = $(this).data('type');  
    friendStr = toString(friendID).concat(friendType); 
    console.log(friendStr); //#2 
    //Loop through & Add 
    if(friendArray.indexOf(friendStr) == -1) { 
     friendArray.push(friendStr); 
    } 
}); 

的friendID标记#1显示了正确#,即5, 4, 6等.. 然而,friendStr标记#2所示["[object Window]Type1", "[object Window]Type2", "[object Window]Type1"]打印时...

如果我删除了toString()函数,控制台会给我一个friendID.concat错误。

有什么建议吗?

回答

7

,因为调用toString(friendID)被调用window.toString()方法,它返回[object Window],你就可以说

String(friendID).concat(friendType); 
//or 
'' + friendID + friendType 
//or just 
friendID + friendType //since friendType is a string 
+0

我该如何去有关更改代码,以便它与#分别打印正确? – SQLNub

+0

或'''+ friendID',或者根本不需要,因为'friendID'是数字,'friendType'是字符串 – Tushar

+0

当限制时间到期时,我会选择你作为答案...非常感谢! – SQLNub