我有一个数组,在那里,我必须只显示第一个数据索引。
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var myBest = fruits.slice(-0, 0); (start index, end index)
但是,无法得到第一个索引,有什么建议吗?
在flatlist中,我必须在某个类中从第二个索引渲染数据。
Here is my code
render() {
// const { profiledata } = this.props.profiledata;
console.log('profiledata is', profiledata);
return (
<View >
<FlatList
style={styles}
showsVerticalScrollIndicator
data={profiledata}
extraData={this.props}
renderItem={({ item }) => (
//code here for UI
)
}
}
))
如何从第二个索引开始在flatlist中循环而不是从slice中获取单独的数组?
有什么建议?
你没有给切片正确的参数。
var myBest = fruits.slice(0, 1); (start index, end index)
返回元素时不包括end index
。 -0
也没有任何意义
endIndex的
Optional.
基于零的索引,在此之前结束提取。此索引处的字符不会包含在内。如果省略endIndex,则slice()将提取到字符串的末尾。如果为负数,则将其视为strLength + endIndex,其中strLength是字符串的长度(例如,如果endIndex为-3,则将其视为strLength - 3)。
如果要修改数组以删除第一个元素,可以使用splice
或shift
方法
如果要从flatList中的第二个位置开始渲染元素,可以使用index属性
<FlatList
style={styles}
showsVerticalScrollIndicator
data={profiledata}
extraData={this.props}
renderItem={({ item, index }) => {
//code here for UI
if (index > 0) {
// code for UI
}
return null;
}}
/>
你正在寻找的功能是shift
此功能允许您取出阵列的第一项,并使用组件中的其余项。
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
console.log('First : ', fruits.shift())
console.log('Others : ', fruits)