2017-04-08 49 views
2

我是學習新手。Javascript數組 - 如何僅打印字符串

在我的代碼: 「https://codepen.io/JonasJsk/pen/qrzLzv

MYAPPLICATION.DisplayNames = function() 
{ 
    var i, len, str; 
    for(i = 0, len = MYAPPLICATION.Names.length, str = ""; i < len; ++i) 
    { 
    str += MYAPPLICATION.Names[i] + "<br>"; 
    } 
    console.log(document.getElementById("content").innerHTML = str); 
} 

我已經宣佈與字符串和數字的JavaScript數組。

1 - 我試圖找出如何我可以專門僅打印輸出字符串到控制檯。

我一直在這幾個小時,請幫幫我!

+0

'的console.log(STR);' – LGSon

+0

對不起 - 不工作 – Jonas

回答

1

使用your code example你可以遍歷數組MYAPPLICATION.Names與方法Array.prototype.forEach()和驗證陣列中的每個元素的typeofstring

var MYAPPLICATION = MYAPPLICATION || {}; 
 
MYAPPLICATION.Names = ["Superman", "Batman", "Flash", 66, 23, 97] 
 
MYAPPLICATION.DisplayNames = function() { 
 
    var content = document.getElementById('content'); 
 
    var ul = document.createElement('ul'); 
 
    MYAPPLICATION.Names.forEach(function (item) { 
 
    if (typeof item === 'string') { 
 
     var li = document.createElement('li'); 
 
     li.appendChild(document.createTextNode(item)); 
 
     ul.appendChild(li); 
 
    } 
 
    }); 
 
    
 
    content.innerHTML = ''; 
 
    content.appendChild(ul); 
 
}
<h2>Lab #2.1</h2> 
 

 
<button type="button" onclick="MYAPPLICATION.DisplayNames()"> 
 
    Get Names 
 
</button> 
 

 
<div id="content"></div>

+0

我只是綁住你的方式,只給出陣列中的第三根弦。 請檢查 - https://codepen.io/JonasJsk/pen/qrzLzv – Jonas

+0

控制檯中的所有名字登錄。什麼情況是,你在做'的document.getElementById(「內容」)的innerHTML = item'和最後,你只能看到最後一個元素 –

+0

更新了代碼示例只有在'與div'元素打印字符串'id'內容 –

0

可以使用typeof操作是這樣的:

var aList = [1, 2, 3, "one", "two", "three"]; 

for(var i = 0; i < aList.length; i++) { 
    if(typeof(aList[i]) == "string") 
    console.log("This is a string : "+aList[i]); 
} 

我希望這有助於。 :)

0

我不明白你的問題...你想從陣列中刪除數字還是隻想在console.log上打印信息?

不管怎樣,試試這個:

var MYAPPLICATION = MYAPPLICATION || {}; 

MYAPPLICATION.Names = ["Superman", "Batman", "Flash", 66, 23, 97] 

MYAPPLICATION.DisplayNames = function() 
{ 
    //MYAPPLICATION.myJSON = JSON.stringify(MYAPPLICATION.Names); 
    var i, len, str; 
    for(i = 0, len = MYAPPLICATION.Names.length, str = ""; i < len; ++i) 
    { 
    if (!isNaN(MYAPPLICATION.Names[i])) { continue; } 
    str += MYAPPLICATION.Names[i] + "<br>"; 
    } 
    console.log(document.getElementById("content").innerHTML = str); 
} 

您也可以刪除console.log,如果你不想在控制檯打印。

像:

var MYAPPLICATION = MYAPPLICATION || {}; 

MYAPPLICATION.Names = ["Superman", "Batman", "Flash", 66, 23, 97] 

MYAPPLICATION.DisplayNames = function() 
{ 
    var i, len, str; 
    for(i = 0, len = MYAPPLICATION.Names.length, str = ""; i < len; ++i) 
    { 
    name = MYAPPLICATION.Names[i] 
    if (!isNaN(name)) { continue; } 
    str += name + "<br>"; 
    } 
    document.getElementById("content").innerHTML = str; 
} 
+0

感謝您的代碼段偉大的工作。 我能問有一件事你的幫助, 我如何去了解,如果我只希望,而不是打印出來的數字? 感謝 – Jonas

+0

改線'如果{繼續(isNaN(名)!); if(isNaN(name)){continue;} }' –

0
MYAPPLICATION.DisplayNames = function() { 
    var stringsOnly = MYAPPLICATION.Names.filter(function(item) { 
     return typeof item === 'string'; 
    }); 
    document.getElementById("content").innerHTML = stringsOnly.join('<br>'); 
}