Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

javascript - 间歇性将对象推入数组(Intermittent pushing of objects to array)

I have a simple block of code that is not working as intended.(我有一个简单的代码块无法正常工作。)

The code parses one array of objects and creates a second with the cumulative sizes found in the former array.(该代码解析一个对象数组,并使用前一个数组中的累积大小创建第二个对象。) The code works excellent on some datasets, but fails on others.(该代码在某些数据集上效果很好,但在其他数据集上却失败。) This is exceedingly odd as all of the datasets were generated using the same tool and have no differences in white space.(这是非常奇怪的,因为所有数据集都是使用同一工具生成的,并且在空白区域没有差异。) The point in which the program fails is pushing the newly created object to the array.(程序失败的地方是将新创建的对象推入数组。) On some datasets it pushes an empty object, while on others it properly pushes it.(在某些数据集上,它会推入一个空对象,而在其他数据集上,它会适当推入它。) Example console logs for failure are:(失败的示例控制台日志是:) Index.html:461 {name: "Chr15", size: 6397009}(Index.html:461 {名称:“ Chr15”,大小:6397009}) Index.html:467 (24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] length: 0(Index.html:467(24)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{ …},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…} ,{…}]长度:0) While logs for functional code: 461 {name: "Chr4", size: 28450051}(记录功能代码时:461 {名称:“ Chr4”,大小:28450051}) Index.html:467 (35) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {name: "NKLS02002208_1", size: 2922554}(Index.html:467(35)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{ …},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…} ,{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…}] 0 :{name:“ NKLS02002208_1”,大小:2922554}) var sizetracker=[]; Blocks.forEach(function (arrayItem) { var tempname=arrayItem.QueChr; var tempsize=parseInt(arrayItem.RefEnd)-parseInt(arrayItem.RefStart); var temptest=false; sizetracker.forEach(function (newarrayItem) { if(newarrayItem.name==tempname){ newarrayItem.size=newarrayItem.size+tempsize; temptest=true; } }); if (temptest==false) { var datapoint = {}; datapoint.name=tempname; datapoint.size=tempsize; console.log(datapoint); sizetracker.push(datapoint); } }); console.log(sizetracker); I have no idea at this point.(我现在还不知道。) There is no appreciable difference in inputs.(输入之间没有明显的差异。)   ask by shakyP translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are modifying sizetracker while you are running a forEach on it.(您正在其上运行forEach时正在修改sizetracker 。)

I think you might expect it to continue processing the items you add to the list, but it doesn't.(我认为您可能希望它继续处理添加到列表中的项目,但事实并非如此。) Run my example code to see the results.(运行我的示例代码以查看结果。) It 'should' have four elements in the list at the end, but it doesn't because forEach only iterates as many times as there are elements in the list at the outset.(它“应该”在列表的末尾有四个元素,但这不是因为forEach只迭代一开始就与列表中的元素一样多的次数。) var list = ['1'] var n = 2 list.forEach(function (el) { if ( list.length < 5 ) { list.push(n.toString()) n++ } }) console.log(list) console.log(list.length)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...