【发布时间】:2019-08-26 14:15:21
【问题描述】:
我想用键值对动态创建一个数据对象。在这个对象中,还有一个名为 features 的数组,并且在这个名为 geometry 的数组元素中还有一个数组坐标。
在特征数组中,标题和内容等属性将动态添加,坐标数组的值也会添加。
如何在坐标数组中添加数据:“几何”:{“坐标”:[]}..?
data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"title": "Day 1",
"content": "This is where some people moved to."
},
"geometry": {
"type": "Point",
"coordinates": [
-73.7949,
40.7282,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "The Next Day",
"content": "This is where some people grooved to."
},
"geometry": {
"type": "Point",
"coordinates": [
-74.3838,
40.9148,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "Amazing Event",
"content": "This is where they went to have fun."
},
"geometry": {
"type": "Point",
"coordinates": [
4.899431,
52.379189,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "1776",
"content": "This where they went when the revolution had begun."
},
"geometry": {
"type": "Point",
"coordinates": [
-71.3489484,
42.4603719,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "1776",
"content": "This where they went when the revolution had begun."
},
"geometry": {
"type": "Point",
"coordinates": [
-71.2272,
42.4473,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "1984",
"content": "So they all came here...and disappeared without a trace!"
},
"geometry": {
"type": "Point",
"coordinates": [
-0.118092,
51.509865,
1
]
}
},
{
"type": "Feature",
"properties": {
"title": "12/22/63",
"content": "Now, this can be quite the scary place."
},
"geometry": {
"type": "Point",
"coordinates": [
-70.2553259,
43.661471,
1
]
}
},
]
}
var data = {
features: []
};
for (piece in pieces){
data.features.push({
type: "Feature",
properties: {title: '{piece.title}' , content: '{piece.content}' },
geometry: {type: "Point"},
});
}
【问题讨论】:
-
你的问题是什么?
-
如果您不尝试任何事情,就很难提供帮助,因为不清楚您遇到的困难是什么。我希望你不是要求别人为你写一个解决方案。
-
这个数据对象是静态填充的,但我想动态地将内容放入其中。
-
Which way is best for creating an object in JavaScript? Is
varnecessary before an object property? 中介绍了创建对象。在Adding members to an existing object 中介绍了向现有对象添加成员...您可以edit 提出您的问题,以使您的问题更清楚,并指出您所做的研究以及遇到的问题。
标签: javascript arrays json object