I am attempting to utilize properties and flatmap to modify an array without altering the original data. I have implemented this method in two different instances within a single dispatch call in Vue.js when transferring data from parent to children components.
- Initially, everything works fine when using the method for the first time. However, when accessing the array for the second time in a different child component, it appears that changes were made to the original data unintentionally. I aim to prevent any modifications to the original array (JS, Vue.js, & TS).
public mapJson(data:any){
let copyData:any = [...data]
const newObjects:Array<MetaObject> = [];
const cb = (e) => {
let element: any = {...e};
if(element.item.parentId.value === 0){
element.item.parentId.value = null;
}
if(element.item.idOnScene){
let split = element.item.idOnScene.split("|");
element.item.modelId = element.item.id.value;
element.item.id.value = split[2];
element.item.name = split[1];
element.item.type = split[0];
}
else{
element.item.modelId = element.item.id.value;
}
let newMeta = {
id: element.item.id.value,
name: element.item.name,
type: element.item.name,
parent: element.item.parentId.value,
modelId: element.item.modelId
}
newObjects.push(newMeta);
e.children && e.children.forEach(cb);
}
copyData.forEach(cb);
const newData : Metadata = {
id: "",
projectId: "2GcXDGLMXA_8_$wW69Hy7E",
author: "",
createdAt: "2019-01-21T13:24:08",
schema: "IFC2X3",
creatingApplication: "20181011_1500(x64) - Exporter 18.4.0.0 - Alternate UI 18.4.0.0",
metaObjects: newObjects
};
return newData;
}