gpt4 book ai didi

Javascript for 循环获取唯一失败

转载 作者:行者123 更新时间:2023-12-03 03:46:32 24 4
gpt4 key购买 nike

我正在研究如何查找 var test 中的独特内容。在 var test 中,有 5 个内容,第一行为 contentId。因此,我有一个 var contentId 来显示 var test 中使用的 contentId 数量。因此,我将每个内容 test 推送到一个数组中,并循环查找哪个 contentid 未被使用。

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]);
}
}
}

太长了;查找 var test 中未使用的唯一 ID。使用的contentid是var contentid

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

最佳答案

您可以尝试看看它是否符合您的期望:

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);
}
})

更新的解决方案

关于Javascript for 循环获取唯一失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com