简体   繁体   English

如何在for循环中初始化数组?

[英]How to initialise an array in a for-loop?

I want create a array like this. 我想创建一个像这样的数组。

itemList = ['1', '2', '3', '4', '5', '6', '7']

But my code's output is 但是我的代码输出是

itemList = ['1,2,3,4,5,6,7']

 var numberList = []; export default class App extends Component<{}> { constructor(props) { super(props); for (let i = 1; i < 8; i++){ numberList.push(i); } numberList = numberList.toString(); this.state = { selectedItem : 2, itemList: [numberList] }; } 

numberList = numberList.toString(); turns your whole array into a string. 将整个数组变成一个字符串。 Then with itemList: [numberList] you create a new array that contains the string as one element. 然后使用itemList: [numberList]创建一个包含该字符串作为一个元素的新数组。

To fix it: 要解决这个问题:

  • Use toString() on every value in the for loop instead on the actual array. 在for循环中的每个值上使用toString() ,而不要在实际数组上使用。
  • Also remove the brackets from itemList: [numberList] . 同时从itemList: [numberList]删除括号。 It should be itemList: numberList instead. 它应该是itemList: numberList

 var numberList = []; for (let i = 1; i < 8; i++){ numberList.push(i.toString()); } console.log(numberList); 

What happens in your code is that you construct the array: 在代码中发生的是构造数组:

[1,2,3,4,5,6,7]

And then call toString on it. 然后调用toString Calling toString on an array will turn the entire array into a string, not turn each element of the array into the string. 在数组上调用toString会将整个数组转换为字符串,而不是将数组的每个元素转换为字符串。 So you want to call the String constructor on each element as you iterate over 1 -> 8. The fixed code can be seen below. 因此,当您在1-> 8上迭代时,您想在每个元素上调用String构造函数。固定代码如下所示。

Note: Not sure what the ultimate goal of this component is, but probably also want to change selectedItem in this.state from 2 -> '2', as I imagine it maps to the elements of itemList which are of type String now and not Number 注:不知道这部分的最终目标是,但可能也想改变selectedItemthis.state 2 - >“2”,因为我想象它映射到的元素itemList这是类型的String ,而不是现在Number

 var numberList = []; export default class App extends Component<{}> { constructor(props) { super(props); for (let i = 1; i < 8; i++){ numberList.push(String(i)); } this.state = { selectedItem : 2, itemList: numberList }; } } 

// the following will convert the whole array as a string, avoid this // numberList = numberList.toString(); //以下内容会将整个数组转换为字符串,请避免// // numberList = numberList.toString();

 var numberList = []; for (let i = 1; i < 8; i++){ numberList.push(i.toString()); } export default class App extends Component<{}> { constructor(props) { super(props); this.state = { selectedItem : 2, itemList: numberList }; } 

There is a way to do it without loop using spread on new Array and map that into values: 有一种方法可以使用new Array上的散布来进行无循环操作并将其map为值:

 /** * Creates an array from start to end where start is a number smaller than end * It takes an optional parameter called step that defaults to 1 * usage example: * range(5,9);// [5, 6, 7, 8, 9] * range(5,9,1.5);// [5, 6.5, 8] */ const range = (start,end,step=1)=>{ const min = start-step return [...new Array(Math.floor((end-min)/step))].map( (val,index)=>min+(step*(index+1)) ); }; console.log(range(5,9)); console.log(range(5,9,1.5)); 

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

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