I have the following code which is meant to generate a new feature with some random values and add it to an array. The problem I have is that through every loop the objects already in the FeatureList array is being overwritten with the new object. I understand that the objects are being passed by ref and have tried the creating local instances of the object before pushing it to the array and also tried using slice to disconnect the tempArr but it still keeps happening.
Please can someone help me !!! thanks
let FeatureList = [];
generateFeature=(type)=>{
var tempArr = [];
var newFeature = {};
newFeature = featureTemplate;
newFeature.properties.title = getRandomTitle(type);
newFeature.properties.description = newFeature.properties.title;
newFeature.properties.url = getUrl(type);
newFeature.properties.type = getType(type);
newFeature.properties.status = getStatus(type);
var newGeoms = []
newGeoms.push(parseFloat(getRandomLatLon()));
newGeoms.push(parseFloat(getRandomLatLon()));
newGeoms.push(getRandomHeight());
newFeature.geometry.coordinates = newGeoms;
newFeature.id = getRandomId(type);
tempArr.push(newFeature);
FeatureList.push(tempArr.slice(0));
tempArr = [];
}
fc =(type)=> {
for (let i = 0; i < 30; i++) {
generateFeature(type);
}
}
fc("Waterpump");
data.features = FeatureList;