2017-07-28 88 views
0

我在計算如何找到var test中的唯一內容。 在var test有5個內容,第一行爲contentId。 因此,我有一個var contentId顯示從var test使用的使用的contentId的數量。因此,我將每個內容test都推送到一個數組中,並循環查找哪些內容未被使用。Javascript for循環獲取唯一失敗

var test = "1#2#Did You Know?#Digital is rapid! A pair of BioCarbon Engineering drones can plant almost 100 ,000 trees a day.#8#Yes\n2#2#Did You Know?#Starting this summer of 2017, Ikea's smart light bulbs will answer voice commands given to Amazon Alexa, Google Assistant, or Apple Siri. The Internet of Things (IoT) is revolutionising everyday appliances!#5#Unknown\n3#2#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n4#2#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n5#3#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n"; 
 

 
//console.log(test); 
 
var result = []; 
 
var allRows = test.split(/\r?\n|\r/); 
 
var week = '2'; 
 
var contentid = '3,2,1'; 
 
var splitContentId = contentid.split(/,/); 
 
var counterLoop = splitContentId.length; 
 
for (var singleRow = 0; singleRow < allRows.length; singleRow++) { 
 
    var rowCells = allRows[singleRow].split('#'); 
 

 
    for (var zeus = 0; zeus < counterLoop; zeus++) { 
 

 
     if (rowCells[0] == splitContentId[zeus] && rowCells[1] == week) { 
 
      console.log(rowCells[0]); 
 
     } 
 
    } 
 
}

TLDR; 查找var test中未使用的唯一ID。所使用的內容識別爲var contentid

的jsfiddle: - https://jsfiddle.net/gyp3awja/

+0

你的問題不明確,什麼是預期的產出? – Dij

+0

@Dij,返回contentId 4和5. – FreedomPride

+0

可以詳細說明嗎?爲什麼4和5? – Dij

回答

1

你可以嘗試,看它是否符合你的期望:

var test = "1#2#Did You Know?#Digital is rapid! A pair of BioCarbon Engineering drones can plant almost 100 ,000 trees a day.#8#Yes\n2#2#Did You Know?#Starting this summer of 2017, Ikea's smart light bulbs will answer voice commands given to Amazon Alexa, Google Assistant, or Apple Siri. The Internet of Things (IoT) is revolutionising everyday appliances!#5#Unknown\n3#2#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n4#2#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n5#3#Did You Know?#Cemex's (a cement company) collaboration platform allows employees to share opinions, knowledge, and best practices. The ideas from every corner of Cemex have led to new initiatives and business outcomes.#5#Unknown\n"; 

var allRows = test.split(/\r?\n|\r/); 
var week = '2'; 
var contentid = '3,2,1'; 
var splitContentId = contentid.split(/,/); 
allRows.forEach(function(row) { 
    var rowData = row.split('#'); 
    var rowId = rowData[0]; 
    var rowWeek = rowData[1]; 

    if (splitContentId.indexOf(rowId) < 0 || rowWeek != week) { 
     console.log(rowId); 
    } 
}) 

更新的解決方案

+0

不知道爲什麼我粘貼它時不工作,讓我讀它。 – FreedomPride

+0

我已添加我的jsfiddle – FreedomPride

+1

嗨,我剛剛解決了問題。 –