[英]My javascript array keeps getting overwritten when trying to add a new object
我使用純JavaScript的nativescript核心。 但是我認為這不是問題所在。 問題的基礎是我試圖將新對象添加到全局數組中,當我這樣做時,以前的數據不斷被新對象覆蓋。
我嘗試了一個普通的array.push({data})和ES6傳播[... array,{data}]。 這兩種方法最終都用新對象覆蓋了數組中的先前數據。
record-page.js
// import statements
// variables
// 'global' array of markers.
var markers = [];
// this function opens a custom modal
function addObsticalTapped(args) {
// close the popup
var page = args.object.page;
var popup = page.getViewById("trailNotesPopup");
isShown = false;
popup.animate({
translate: {
x: 0,
y: 500
},
duration: 300,
curve: enums.AnimationCurve.easeInOut
});
var mainView = args.object;
var context = {};
geolocation
.getCurrentLocation({
desiredAccuracy: Accuracy.high
})
.then(loc => {
curLoc = loc;
});
mainView.showModal(obsticalModal, context, addObsticalIcon, false);
}
exports.addObsticalTapped = addObsticalTapped;
// callback function when the modal is closed
function addObsticalIcon(didConfirm, data) {
if (didConfirm) {
// this is where the problem is, the markers array is being overwritten
// when adding another marker
markers = [...markers, {
type: "obstical",
location: {
lat: curLoc.latitude,
lng: curLoc.longitude
},
data: data,
trail_id: ""
}];
map.addMarkers([{
id: markerID,
lat: curLoc.latitude,
lng: curLoc.longitude,
//icon: "res://obstical_icon"
iconPath: "./icons/obstical_icon_marker.png"
}]);
markerID++;
console.log(JSON.stringify(markers));
} else {
console.log("closed");
}
}
obstical-modal.js
function onShownModally(args) {
const context = args.context;
closeCallback = args.closeCallback;
const page = args.object;
vm = observableModule.fromObject(context);
vm.set("oneSelected", oneSelected ? oneOn : oneOff);
vm.set("threeSelected", threeSelected ? threeOn : threeOff);
vm.set("sixSelected", sixSelected ? sixOn : sixOff);
vm.set("nineSelected", nineSelected ? nineOn : nineOff);
page.bindingContext = vm;
}
exports.onShownModally = onShownModally;
function onCancel(args) {
closeCallback(false, {});
}
exports.onCancel = onCancel;
function onSubmit(args) {
var page = args.object.page;
var textField = page.getViewById("info");
data.info = textField.text;
closeCallback(true, data);
}
exports.onSubmit = onSubmit;
我期望發生的事情:障礙物1的難度為1,信息“ hello world”,然后將其添加到數組中,數組是正確的。 然后,我添加了另一個難度為3的障礙物和“ hello代碼”信息,將其添加到數組后,數組看起來像:
[{"type":"obstical","data":{"difficulty":3,"info":"hello code"}},{"type":"obstical","data":{"difficulty":3,"info":"hello code"}}]
我打算將此作為注釋,但想向您展示一個我認為您通過編寫一些簡化版本的代碼來做錯事的示例。
const data = {
difficulty: '1',
info: 'hello code',
};
const markers = [];
markers.push({
type: 'obstical',
data: data,
});
// Here is the problem. The problem is not related to the array adding
data.difficulty = '3';
markers.push({
type: 'obstical',
data: data,
});
該問題與如何添加到數組無關,但與原始數據對象有關。 解決方案如下
const data = {
difficulty: '1',
info: 'hello code',
};
const markers = [];
markers.push({
type: 'obstical',
data: data,
});
// create a new object instead of mutating the existing one
const newData = {
...data,
difficulty: '3',
};
markers.push({
type: 'obstical',
data: newData,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.