2017-07-28 66 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

嗨,我刚刚解决了问题。 –