list: pois.filter((item) => {
return item.photos.length
}).map((item) => {
return {
type: item.type,
img: item.photos[0].url,
name: item.name,
price: Math.floor(Math.random() * 100),
comment: Math.floor(Math.random() * 1000),
rate: Math.floor(Math.random() * 10 / 2),
scene: item.tag,
status: '可訂明日',
location: item.location,
module: item.type.split(';')[0]
}
}),
filter 過濾,當item 元素的 photos 里面有圖片的時候,我們才要,沒有就不要了
也可以簡寫
pois.filter(item=> item.photos.length) 滿足條件的我就要,不滿足的就不要
--------------------------------------------------------------------
接口數據映射 map()函數
map((item) => {
return {
type: item.type,
img: item.photos[0].url,
name: item.name,
price: Math.floor(Math.random() * 100),
comment: Math.floor(Math.random() * 1000),
rate: Math.floor(Math.random() * 10 / 2),
scene: item.tag,
status: '可訂明日',
location: item.location,
module: item.type.split(';')[0]
}
})
最后返回的數據結構 是你定義好的 type,img,name 等等
Q:那么為什么要使用map呢?
A:如果,后端改api接口字段了,前端之前的字段沒有改,那么我可以能一次要改很多地方,而且數據格式可能還不一樣的;
前端一定要有自己的數據字段,使用map() 直接接口數據映射,我后面就只改映射時的字段就好!
切記,切記,坑進去了,就要爬出來!