简体   繁体   English

同一索引上的多个值

[英]Multiple values on same index

So I am doing a small code test at a place called Experis.所以我在一个叫做 Experis 的地方做一个小代码测试。 And while the assigment itself was fine, the problem lies when I try to fetch the input from their servers.虽然分配本身很好,但问题出在我尝试从他们的服务器获取输入时。 There are multiple inputs that needs to be tested, however instead of being sent in an array, they are all sent at once.有多个输入需要测试,但是它们不是在数组中发送,而是一次发送。 Which itself is fine, I just use " line.split(' ') " to split every sentence into its own word.这本身很好,我只是使用“ line.split(' ') ”将每个句子拆分成自己的单词。 However after I try to call the first part with "line[0] " I get a very weird result, instead of just sending the first word, it sends the first letter of every word.然而,在我尝试用"line[0] ”调用第一部分后,我得到了一个非常奇怪的结果,它不只是发送第一个单词,而是发送每个单词的第一个字母。 The inputs are:输入是:

at ordeals abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd在考验 abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (line) => {
    var nums = line.split(' ');
    
    console.log(nums);
});

Expected result: at预期结果:在

Actual result: aoaaa实际结果:aoaaa

I have tried to parse the data, turn it into an array, using different kinds of spliting, but nothing I try seem to work properly.我试图解析数据,将其转换为数组,使用不同类型的拆分,但我尝试的任何操作似乎都无法正常工作。 I have contacted their support team, but since it is weekend they don't seem to answer.我已经联系了他们的支持团队,但由于是周末,他们似乎没有回复。 And at this point I am desperate for help.在这一点上,我迫切需要帮助。

Edit:编辑:

logging line gave me at ordeals abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd in type string,string,string,string,string.日志行给了我ordeals abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd类型字符串、字符串、字符串、字符串、字符串的考验。

While logging nums gives me: [ 'at' ] [ 'ordeals' ] [ 'abcdefghijklmnopqrstuvwxyz' ] [ 'abcdefghijklmabcdefghijklm' ] [ 'abcdABCDabcd' ] in type object,object,object,object,object.虽然记录 nums 给我:[ 'at' ] [ 'ordeals' ] [ 'abcdefghijklmnopqrstuvwxyz' ] [ 'abcdefghijklmabcdefghijklm' ] [ 'abcdABCDabcd' ] in type object,object,object,object,object. Which makes it so that if I log nums[0] then I get the entirety of input again in string,string,string,string,string.这样一来,如果我记录 nums[0],那么我将以字符串、字符串、字符串、字符串、字符串的形式再次获得完整的输入。

try somthing like that尝试那样的事情

var nums = line.split(/\s/); var nums = line.split(/\s/);

  1. Try logging what is JSON.stringify({line}) and JSON.stringify({nums})尝试记录什么是JSON.stringify({line})JSON.stringify({nums})
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (line) => {
    var nums = line.split(' ');
    
    // expected: '{line: "at ordeals ... " }'
    console.log(JSON.stringify({line}));
    // expected: '{nums: ["at", "ordeals", ...] }'
    console.log(JSON.stringify({nums}));
});

JSON and object is to check if you understand its correctly and escape \n, \t, etc JSON 和 object 是检查你是否理解正确并转义 \n, \t 等

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

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