我试图动态创建for循环内的对象的属性,同时删除旧的属性。创建对象属性替换循环内的每个对象属性
这是源:
var input= { contents:
[ { source: 'source',
sentiment: "positive",
user_location_latitude: null,
user_location_longitude: null
}
//...
]
这就是我想要的:
var input= { contents:
[ { source: 'source',
sentiment: "positive",
location: {long: null, lat: null}
//with the contents of user_location_latitude and longitude
}
//...
]
这是我的代码:
for(var i=0 ;i< input.contents.length; i++){
input.contents[i] = {"location": {"lat": input.contents[i].user_location_latitude,
"lon":input.contents[i].user_location_longitude}}
delete input.contents[i].user_location_latitude;
delete input.contents[i].user_location_longitude;
}
而且我越来越:
{ contents:
[ { location: [Object] },
{ location: [Object] },
{ location: [Object] },
{ location: [Object] },
{ location: [Object] },
{ location: [Object] }
//...
]
}