简体   繁体   English

使用for循环用对象填充数组

[英]Using a for loop to populate an array with objects

I have a function that takes an array, consisting of 3 sets of strings. 我有一个函数需要一个数组,其中包含3组字符串。 For each set of strings, the function should spit out 2 resulting integers/numbers. 对于每组字符串,该函数应吐出2个结果整数/数字。

Link to the jsfiddle of work in progress So if the input is 链接到进行中的jsfiddle,因此如果输入为

["10:00AM-12:30PM","02:00PM-02:45PM","09:10AM-09:50AM"]

I'm trying to get the function, by using a for-loop, to spit out 2 minute counts for each element of the array (a total of 6 minute counts, 2 per string). 我正在尝试通过使用for循环来获取该函数,以为该数组的每个元素吐出2分钟的计数(总计6分钟的计数,每个字符串2个)。

I'm thinking I need the results stored in an object? 我在想是否需要将结果存储在对象中? Or maybe an array, consisting of objects? 还是由对象组成的数组? I'm a little bit confused here. 我在这里有点困惑。

I'm confused as to how to organize it so that whatever is returned from the function, I can easily access it. 我对如何组织它感到困惑,以便从函数返回的任何内容都可以轻松访问。

So maybe an array of 3 objects is returned is the best way to do it, with each object consisting of: 因此,最好返回包含3个对象的数组,这样做是最好的方法,每个对象包括:

1st identifier key: an identifier of some sort (perhaps using the [i] from the for loop),
2nd key/property time1min: with the value being time1min (which is the 1st minute count),
3rd property time2min: with the value being time2min for that string.

As you can see from my jsfiddle above, I'm lost as to how to output to an object, or to an array of objects. 从上面的jsfiddle中可以看到,我对如何输出到对象或对象数组一无所知。

results is an array; 结果是一个数组; so results[0].time1min Try using console.log(results); 所以results[0].time1min尝试使用console.log(results); instead. 代替。 It plays nicer than alerts. 它比警报更好。

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

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