React native 如何制作Object.map?
我有一系列的数据。我使用data.map的7个项目。我在firease上加载了这个数组,现在不能这样调用它。因为不是这样,所以数组已经存在于对象中 问题 如何为对象创建data.map。此外,我需要传输数据。特别是:id、姓名、信息、车牌号。里面是应该在data.map中的ImageCard 示例对象:React native 如何制作Object.map?,react-native,React Native,我有一系列的数据。我使用data.map的7个项目。我在firease上加载了这个数组,现在不能这样调用它。因为不是这样,所以数组已经存在于对象中 问题 如何为对象创建data.map。此外,我需要传输数据。特别是:id、姓名、信息、车牌号。里面是应该在data.map中的ImageCard 示例对象: Object { "0": Object { "id": 0, "image": "/images/Stargate.jpg", "info": "Stargate
Object {
"0": Object {
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
},
您可以使用
Object.keys
从对象中提取关键帧,它将返回所有关键帧的数组,然后映射此数组
像这样,
const obj={
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
}
let keys = Object.keys(obj);
keys.map(item=>{
//..... do your stuff from object like,
let y=obj[item]
// or whatever
}
有多种方法可以做到这一点
Jaydeep Galani提到的一个
另一种方法是使用代理
const obj={
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through
Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
}
const newObj = new Proxy(obj,{
get: (target, prop)=>{
let newArr = [];
if(prop === 'map'){
// then first convert target into array
Object.keys(target).foreach(item => {
newArr.push({item:target[item]})
})
// then apply map function to that and return the result
return newArr.map((item)=>{
//your code goes here like
return<div>{item.info}</div>
})
}
}
})
const对象={
“id”:0,
“image”:“/images/Stargate.jpg”,
“信息”:“星际之门”是1994年通过
米高梅(米高梅)和卡罗尔科电影公司,
“latlng”:对象{
“纬度”:53.6937,
“经度”:-336.1968,
},
“名称”:“星际之门”,
“年份”:“1994年”,
}
const newObj=新代理(obj{
获取:(目标、道具)=>{
设newArr=[];
如果(道具=='map'){
//然后首先将目标转换为数组
Object.keys(target.foreach)(项=>{
newArr.push({item:target[item]})
})
//然后对其应用map函数并返回结果
返回newArr.map((项)=>{
//你的代码在这里就像
返回{item.info}
})
}
}
})
多种方式:对象。键
,对象。值
,对象。条目
都是将对象转换为数组的可能选项