Javascript 如何从另一个对象在json对象中添加数据?
我有一个数据对象,其中包含一些数据,现在我想创建另一个对象Javascript 如何从另一个对象在json对象中添加数据?,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我有一个数据对象,其中包含一些数据,现在我想创建另一个对象mapdata2,它与数据具有相同的结构。但是我的代码不起作用,并且显示了一些语法错误。 我已经创建了mapdata2对象和其中的空特征数组。 它显示了一个错误: 类型错误:i.未定义特征 数据={“类型”:“FeatureCollection”, “特点”:[ { “类型”:“功能”, “财产”: { “标题”:“ABC”,“起始日期”:1100,“结束日期”:1200,“纬度”:60.814,“经度”:11.845,“内容”:“内容
mapdata2
,它与数据具有相同的结构。但是我的代码不起作用,并且显示了一些语法错误。
我已经创建了mapdata2
对象和其中的空特征数组。它显示了一个错误: 类型错误:i.未定义特征
数据={“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“财产”:
{
“标题”:“ABC”,“起始日期”:1100,“结束日期”:1200,“纬度”:60.814,“经度”:11.845,“内容”:“内容”
},
“几何学”:
{
“类型”:“点”,“坐标”:[60.814,11.845,1]
}
},
{
“类型”:“功能”,
“财产”:
{
标题:“XYZ”,“起始日期”:1100,“结束日期”:1200,“纬度”:40.814,“经度”:15.845,“内容”:“内容”
},
“几何学”:
{
“类型”:“点”,“坐标”:[40.814,15.845,1]
}
},
]
}
mapdata2={
“类型”:“FeatureCollection”,
“功能”:[]
};
对于(数据中的i){
控制台日志(i);
mapdata2.features.push({
键入:“功能”,
属性:{title:i.features.properties.title,开始日期:i.features.properties.startDate,结束日期:i.features.properties.endDate纬度:i.features.properties.latitude,经度:i.features.properties.longitude,内容:i.features.properties.content},
几何体:{类型:“点”,坐标:i.features.geometry.coordinates}
})
}
console.log(mapdata2);
您似乎想要复制对象。您可以简单地执行以下操作
var mapdata2 = JSON.parse(JSON.stringify(data));
顺便说一下,您得到了错误“TypeError:i.features未定义”,因为i是表示对象键的字符串。它将采用值“type”和“features”
要循环数组data.features中的项,应执行以下操作:
for (var i=0;i<data.features.length;i++) {
var item = data.features[i];
}
for(var i=0;i发生这种情况是因为您试图访问数据中每个i
的功能,但第一个i
是“type”
,其中没有功能
所以我修改了你的代码,这样你就可以只在“特性”上进行迭代,对于每一个特性,你都做了你所做的,现在它开始工作了
数据={
“类型”:“FeatureCollection”,
“特点”:[{
“类型”:“功能”,
“财产”:{
“标题”:“ABC”,
“起始日期”:1100,
“结束日期”:1200,
“纬度”:60.814,
“经度”:11.845,
“内容”:“内容”
},
“几何学”:{
“类型”:“点”,
“坐标”:[60.814,11.845,1]
}
},
{
“类型”:“功能”,
“财产”:{
“标题”:“XYZ”,
“起始日期”:1100,
“结束日期”:1200,
“纬度”:40.814,
“经度”:15.845,
“内容”:“内容”
},
“几何学”:{
“类型”:“点”,
“坐标”:[40.814,15.845,1]
}
},
]
}
mapdata2={
“类型”:“FeatureCollection”,
“功能”:[]
};
data.features.forEach((feature)=>{
mapdata2.features.push({
键入:“功能”,
特性:{
标题:feature.properties.title,
startDate:feature.properties.startDate,
endDate:feature.properties.endDate,
id:feature.properties.id,
纬度:feature.properties.latitude,
经度:feature.properties.longitude,
内容:feature.properties.content
},
几何图形:{
键入:“点”,
坐标:feature.geometry.coordinates
}
})
});
console.log(mapdata2);
一个维护您的风格的“快速修复”:
for(i of data.features) {
console.log(i)
mapdata2.features.push({
type:"Feature",
properties : {
title: i.properties.title,
startDate: i.properties.startDate,
endDate: i.properties.endDate,
id: i.properties.id,
latitude: i.properties.latitude,
longitude: i.properties.longitude,
content: i.properties.content },
geometry : {
type: "Point",
coordinates: i.geometry.coordinates
}
})
}
如果只想将json列表复制到mapdata2,则根本不需要映射或循环
mapdata2.features = data.features
console.log(mapdata2);
这将完成您希望完成的任务,而无需进行任何循环
数据={“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“财产”:
{
“标题”:“ABC”,“起始日期”:1100,“结束日期”:1200,“纬度”:60.814,“经度”:11.845,“内容”:“内容”
},
“几何学”:
{
“类型”:“点”,“坐标”:[60.814,11.845,1]
}
},
{
“类型”:“功能”,
“财产”:
{
标题:“XYZ”,“起始日期”:1100,“结束日期”:1200,“纬度”:40.814,“经度”:15.845,“内容”:“内容”
},
“几何学”:
{
“类型”:“点”,“坐标”:[40.814,15.845,1]
}
},
]
}
mapdata2={
“类型”:“FeatureCollection”,
“功能”:[]
};
mapdata2.features=data.features
console.log(mapdata2);
此处有一些错误:
in
for-bucle中的关键字会迭代关键点,而不是值,据我所知,复制数据时您需要这样做
- 您试图访问数组的属性
属性
,而不首先访问该数组的索引
将代码的最后一部分更改为:
for(const feature of data.features){
mapdata2.features.push({
type:"Feature",
properties : { title: feature.properties.title, startDate: feature.properties.startDate, endDate: feature.properties.endDate, latitude: feature.properties.latitude, longitude: feature.properties.longitude, content: feature.properties.content },
geometry : { type: "Point", coordinates: feature.geometry.coordinates }
})
}
console.log(mapdata2);
应该做这项工作:)
但是,如果您只是打算复制数据,我建议您:
mapdata2 = {...data}
console.log(mapdata2)
谢谢@奥姆里·阿提亚。现在它开始工作了。我有个问题。在我的数据中,几何体中还有一个数组“坐标”。如果我想在这个数组中动态添加数据,那么我需要在上面再创建一个数组吗@默罕默德哈桑
mapdata2 = {...data}
console.log(mapdata2)