我在计算如何找到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/
你的问题不明确,什么是预期的产出? – Dij
@Dij,返回contentId 4和5. – FreedomPride
可以详细说明吗?为什么4和5? – Dij