我有一个根据日期存储数据的数组,如何在特定日期渲染数据?
我尝试使用array[0].Data
并成功了,但是我真正想要实现的是类似传递prop“ array(Jul)”之类的东西,它在7月显示了数据。有没有一种方法可以实现此目的?
let array = [{Month: "Jul", Data: "this is my data", Place: "UK"},{Month: "Aug", Data: "this is my data2", Place: "USA"}]
<View >
<Text small bold white> {array(Jul).Data} </Text>
</View>
答案 0 :(得分:1)
您可以使用find:
array.find(x => x.Month === 'Jul').Data
答案 1 :(得分:1)
您可以将数组转换为对象
let array = [{Month: "Jul", Data: "this is my data", Place: "UK"},{Month: "Aug", Data: "this is my data2", Place: "USA"}]
const newData = array.reduce((acc, item) => (acc[item.Month] = item.Data, acc),{});
console.log(newData)
console.log(newData['Jul'])
并访问以下数据
<View >
<Text small bold white> {newData['Jul']} </Text>
</View>