我已经下载了一个json文件,其中包含了许多我不需要的信息,最终目的是将表格呈现到只包含部分数据项的html页面。
data.json看起来像
{ "events": [
{
"id": "181101322507029",
"name": "Carnival Special",
"type": "public",
"coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0.jpg?oh=b9eC",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-jpg?oh=32c4574",
"description": "Lots and lots of info blah blah",
"distance": "1546",
"startTime": "2017-11-18T22:00:00+0000",
"endTime": "2017-11-19T03:00:00+0000",
"timeFromNow": 476169,
"isCancelled": false,
"isDraft": false,
"category": null,
"ticketing": {
"ticket_uri": "https://nicevenue.co.uk/event/lime-of-your-life/"
},
"place": {
"id": "517137361697082",
"name": "Nice Venue",
"location": {
"city": "Nice City",
"country": "United Kingdom",
"latitude": 66.3445334,
"longitude": -4.7356667928,
"street": "The Blah Blah, Blah, Blah Road",
"zip": "AB1 2CD"
}
},
"stats": {
"attending": 68,
"declined": 0,
"maybe": 177,
"noreply": 336
},
"venue": {
"id": "517137361697082",
"name": "Nice Venue",
"about": "Situated in the blah blah aims to provide a friendly atmosphere, gourmet food and fine cocktails.",
"emails": [
"nicevenue@googlemail.com"
],
"coverPicture": "https://scontent.xx.fbcdn.net/v/t31.0-8/s78_o.jpg?oh=8f62cb6CB2",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/c0.16n.jpg?oh=8ef5df00f31&oe=5AAF3935",
"category": "Bar",
"categoryList": [
"Bar",
"Restaurant"
],
"location": {
"city": "Nice City",
"country": "United Kingdom",
"latitude": 65.138604292331,
"longitude": -9.73566d867928,
"street": "The Blah Blah, Blah, Blah Road",
"zip": "AB1 2CD"
}
}
},
{
"id": "1896504087342309",
"name": "Mr Tea + Dj Dr SJ",
"type": "public",events
"
"
etc....
我需要遍历这些数据并创建一个像
这样的新对象(或数组){"id": 123456 [
{
"StartTime": "2017-11-18T22:00:00+0000",
"description": "Lots and lots of info blah blah",
"place": {
"id": "517137361697082",
"name": "Nice Venue",
"location": {
"city": "Nice City",
"country": "United Kingdom",
"latitude": 66.3445334,
"longitude": -4.7356667928,
"street": "The Blah Blah, Blah, Blah Road",
"zip": "AB1 2CD"
}]
}
然后,我想使用快速模板迭代此对象,以将信息呈现到html中的表标记。
我已经在使用hasProperty或使用低破折号_.Map查看For in循环,但我真的不明白我是否正确地执行此操作,甚至以最佳方式执行我想要做的事情。感谢
答案 0 :(得分:2)
var resObj = JSON.parse("your json data");
resObj.forEach(function(event)){
var id = event.id;
var place = event.place;
//so on...and create new objects here.
}
然后您可以在html中加载数据或从循环中创建一个html字符串并将其附加到主div。
答案 1 :(得分:1)
如果您使用的是ES6或更高版本,您可以使用新对象创建一个新数组:
如果您不使用ES6,则lodash将具有_map
我将通过使用将firstname转换为name的示例来保持简单:
var test = {
"students": [
{
"firstname": "ian",
"age": 25
},
{
"firstname": "Rick",
"age": 25
}
]
}
var newList = test.students.map(data => ({
name: data.firstname ,
age: data.age
}));
console.log(newList)