使用 for 创建年龄数组
Create an Age Array using for
我正在创建一个年龄选择器,我已经设置了所有轮播和屏幕,但是我在将数组设置为从 20 岁到 80 岁时遇到问题,我所做的测试是这样的
const dataAge = [
{age: '21'},
{age: '22'},
{age: '23'},
{age: '24'},
{age: '25'},
{age: '26'},
{age: '27'},
{age: '28'},
{age: '29'},
{age: '30'},
{age: '31'},
{age: '32'},
{age: '33'},
{age: '34'},
{age: '35'},
{age: '36'},
];
但我无法使用 FOR 来解决这个问题
这是要插入数组的代码
<View
style={{
backgroundColor: '#FFF',
flex: 0.18,
borderRadius: 10,
margin: 10,
}}>
<View
style={{
flexDirection: 'row',
flex: 1,
justifyContent: 'space-between',
}}>
<Text style={{fontSize: 15, margin: 10, alignItems: 'flex-start'}}>
Age
</Text>
<Text
style={{
fontSize: 10,
alignItems: 'flex-end',
margin: 10,
}}>
years old
</Text>
</View>
{/* Age picker */}
<View style={{alignItems: 'center'}}>
<Carousel
data={dataAge}
renderItem={renderItemAge}
sliderWidth={300}
itemWidth={30}
horizontal={true}
firstItem={5}
enableMomentum={true}
/>
</View>
<View style={styles.TriangleShapeView}></View>
</View>
感谢帮助
试试这个:
let age = 20;
let dataAge = Array.from({ length: 61 }, () => {
let obj = { age: age.toString() };
age++;
return obj;
});
console.log(dataAge);
只需遍历数组并将对象推送到它:
let dataAge = [];
for (let i = 20; i <= 80; i++) {
dataAge.push({ age: i.toString() });
}
我正在创建一个年龄选择器,我已经设置了所有轮播和屏幕,但是我在将数组设置为从 20 岁到 80 岁时遇到问题,我所做的测试是这样的
const dataAge = [
{age: '21'},
{age: '22'},
{age: '23'},
{age: '24'},
{age: '25'},
{age: '26'},
{age: '27'},
{age: '28'},
{age: '29'},
{age: '30'},
{age: '31'},
{age: '32'},
{age: '33'},
{age: '34'},
{age: '35'},
{age: '36'},
];
但我无法使用 FOR 来解决这个问题
这是要插入数组的代码
<View
style={{
backgroundColor: '#FFF',
flex: 0.18,
borderRadius: 10,
margin: 10,
}}>
<View
style={{
flexDirection: 'row',
flex: 1,
justifyContent: 'space-between',
}}>
<Text style={{fontSize: 15, margin: 10, alignItems: 'flex-start'}}>
Age
</Text>
<Text
style={{
fontSize: 10,
alignItems: 'flex-end',
margin: 10,
}}>
years old
</Text>
</View>
{/* Age picker */}
<View style={{alignItems: 'center'}}>
<Carousel
data={dataAge}
renderItem={renderItemAge}
sliderWidth={300}
itemWidth={30}
horizontal={true}
firstItem={5}
enableMomentum={true}
/>
</View>
<View style={styles.TriangleShapeView}></View>
</View>
感谢帮助
试试这个:
let age = 20;
let dataAge = Array.from({ length: 61 }, () => {
let obj = { age: age.toString() };
age++;
return obj;
});
console.log(dataAge);
只需遍历数组并将对象推送到它:
let dataAge = [];
for (let i = 20; i <= 80; i++) {
dataAge.push({ age: i.toString() });
}