我實際上是從谷歌日歷 API 獲取公共假期的資料,我想在日歷中顯示它們。為此,我首先從 google API 獲取資料,然后我嘗試創建一個新的物件陣列,用于存盤我需要從 google API 日歷中獲取的資料。問題是我能夠創建一個新的物件陣列,但無法為從谷歌日歷 API 獲取的整個資料添加新物件。我嘗試使用回圈但不起作用。
我的代碼
let google;
useEffect(() => {
axios.get('https://www.googleapis.com/calendar/v3/calendars/en.australian#[email protected]/events?key=personalkey')
.then(res => {
const data = res.data.items;
console.log("Google api calendar data", data)
for (let i = 0; i < data.length; i ) {
data.map((item) => {
google = {
"id": item.id,
"title": item.summary,
"location": "Australia",
"start": new Date(moment(item.start.date)),
"end": new Date(moment(item.end.date)),
"description": item.description,
"durationday": 'Fulllday',
"sameId": null
}
})
}
console.log("goggle making array of object inside promise", google)
})
.catch(err => { console.log('Google api calendar error', err) })
})
我的控制臺

google API 內部資料供參考

uj5u.com熱心網友回復:
現在您正在回圈遍歷這些專案兩次。forloop 和map兩者都回圈遍歷陣列。map是您想要的,如果想要修改陣列,則使用它,但它要求您回傳要放置在新陣列中的每個物件。您應該在array.prototype.map 中閱讀有關如何使用 map 的資訊。所以我認為你正在嘗試做的是以下內容:
useEffect(() => {
axios.get('https://www.googleapis.com/calendar/v3/calendars/en.australian#[email protected]/events?key=personalkey')
.then(res => {
const data = res.data.items.map((item) => {
return {
"id": item.id,
"title": item.summary,
"location": "Australia",
"start": new Date(moment(item.start.date)),
"end": new Date(moment(item.end.date)),
"description": item.description,
"durationday": 'Fulllday',
"sameId": null
}
})
console.log("Your new array of modified objects here", data)
})
.catch(err => { console.log('Google api calendar error', err) })
}, [])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359496.html
標籤:javascript 数组 反应 目的 谷歌日历 API
