我想向我的数组对象添加新的key/value
,但出现此错误:
Uncaught TypeError: Cannot set property 'english_province' of undefined
addresses[0].english_province = "test"
or
addresses[0]['english_province'] = "test"
我的功能:
function prepationAddresses(){
var addresses = app.vueList;
console.log('_addresses',addresses)
console.log('_addresses.length',addresses.length)
console.log('_addresses.0',addresses[0])
for(var i = 0 ; i < addresses.length ; ++i){
var item = addresses[i];
var mGeocode = null
getGeocode(item.x,item.y).then(function (_mGeocode){
console.log('_mGeocode',_mGeocode)
mGeocode = _mGeocode
});
setTimeout(() => {
console.log('mGeocode',mGeocode.results[0])
console.log('_addresses',addresses)
console.log('_addresses item',item)
//console.log('mGeocode',mGeocode.results[0].address_components[4])
addresses[i].english_province = mGeocode.results[4].address_components[1].long_name
addresses[i].english_city = mGeocode.results[4].address_components[0].long_name
}, 1000);
}
setTimeout(() => {
// submitNewRequest(addresses)
console.log(addresses);
}, 5000);
}