我有一系列数据。我使用data.map的7个项目。我在火上加载了这个数组,现在我不能这样称呼它。因为这不是数组已经在对象中。
问题。
如何进行data.map的对象映射。而且,我需要传输数据。具体:ID,名称,信息,latlng。内部是应该在data.map中的想象卡。
示例对象:
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>
})
}
}
})