简体   繁体   English

连续遍历给定数量的数据集

[英]Continuously loop through a dataset given number of items

I have a certain dataset, which looks like this: 我有一个特定的数据集,看起来像这样:

3 # Number of test cases 3 # Test case 1 has 3 names ALFRED SINGH JOHN 6 # Test case 2 has 6 names ALPHA NITRO ROB RICHARD ANON POPE 1 # Test case 3 has 1 name FELIX

So i have stringified this dataset, and my problem is looping through the entire dataset based on number of testcases, and I also have to consider number of names in each test case. 所以我对这个数据集进行了字符串化,而我的问题是根据测试用例的数量遍历整个数据集,而且我还必须考虑每个测试用例中名称的数量。 i am using javascript for this and this is my current code. 我为此使用JavaScript,这是我当前的代码。 it can only do the first test case: 它只能做第一个测试用例:

 var example = '3\\n3\\nALFRED\\nSINGH\\nJOHN\\n6\\nALPHA\\nNITRO\\nROB\\nRICHARD\\nANON\\nPOPE\\n1\\nFELIX' run(example) function run(input) { var lines = input.split('\\n') var testCases = lines[0] lines.shift() var n = lines[0] lines.shift() for (var i =0; i < n; i += 1) { var names = lines[i] console.log(names) } } 

My problem is looping through the entire dataset while respecting number of test cases and names. 我的问题是在尊重测试用例和名称数量的同时遍历整个数据集。

Answers Multiple correct answers. 答案多个正确答案。 Going with JaromandaX for identifying the problem 1st. 与JaromandaX一起识别问题1。

ADDITIONAL PROBLEM 其他问题

So the answers seem to return a list of all names without considering the test cases. 因此,答案似乎返回了所有名称的列表,而不考虑测试用例。 The aim is to group the names in respect to their test cases. 目的是针对测试用例对名称进行分组。 so a structure like this (theoretically): 所以这样的结构(理论上):

{
    "1": ["ALFRED", "SINGH", "JOHN"],
    "2": [...],
    "3": ["FELIX"]
}

JSFiffler for quick reference : https://jsfiddle.net/rj1405/vttejren/1/ JSFiffler快速参考: https ://jsfiddle.net/rj1405/vttejren/1/

You need to loop through the test cases and have to remove the elements using shift() . 您需要遍历测试用例,并且必须使用shift()删除元素。

 var example = `3 3 ALFRED SINGH JOHN 6 ALPHA NITRO ROB RICHARD ANON POPE 1 FELIX`; run(example) function run(input) { var lines = input.split('\\n') var testCases = lines.shift(); for(var x = 0; x < testCases; x++){ var numberOfNames = lines.shift(); for (var i = 0; i < numberOfNames; i++) { var names = lines.shift(); console.log(names); } } } 

Use an outer loop for the test cases - inner loop needs a slight change too 对测试用例使用外部循环-内部循环也需要稍作更改

 var example = '3\\n3\\nALFRED\\nSINGH\\nJOHN\\n6\\nALPHA\\nNITRO\\nROB\\nRICHARD\\nANON\\nPOPE\\n1\\nFELIX'; function run(input) { var lines = input.split('\\n') var testCases = lines.shift(); var obj = {}; for (var x = 0; x < testCases; x++) { var testCase = obj[x+1] = []; var numberOfNames = lines.shift(); for (var i = 0; i < numberOfNames; i += 1) { testCase.push(lines.shift()); } } return obj; } console.log(run(example)); 

note, using lines.shift() to "read" the values from lines, because shift returns the line that was shifted 请注意,使用lines.shift()从行中“读取”值,因为shift返回被移位的行

You need to loop through all of test cases. 您需要遍历所有测试用例。 This is the code: 这是代码:

 var example = // 'the dataset above (stringified)' run(example) function run(input) { var lines = input.split('\\n') var testCases = lines[0] lines.shift() for(var x = 0; x < testCases; x++){ var numberOfNames = lines[0] lines.shift() for (var i =0; i < numberOfNames; i += 1) { var names = lines.shift() console.log(names) } } } 

EDIT: You have to remove those names from array :) 编辑:您必须从数组中删除这些名称:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM