The following example is working correctly, yielding the desired output. However, I'm wondering if there's a more concise way to do this copy operation by excluding the key/value pairs for keys created
and updated
from the data
object.
const actionMode = 'update'; // 'create';
const dataFromParent = {
itemid: "104",
itemname: "insurance",
itemtype: "expense",
unitofmeasure: "$/yr",
created: "2021-04-14 05:59:30.097",
updated: "2021-04-14 05:59:30.097"
}
const data = { ...dataFromParent }; // make a shallow copy to avoid changing original object values
if (actionMode == 'update') {
delete data.created;
delete data.updated;
}
console.log(JSON.parse(JSON.stringify(dataFromParent)));
console.log(JSON.parse(JSON.stringify(data)));
Using the recommendation posted by @vdegenne on 11/23/2018 in this post I tried:
let data = {};
if (actionMode == 'update') {
data = (({ created, updated, ...o }) => o)(dataFromParent);
}
which causes a "Failed to compile" result with these errors:
EDIT:
This modification gives the desired result and does not have any "no-unused-vars"
errors:
let data = {};
if (actionMode == 'update') {
// eslint-disable-next-line no-unused-vars
const { created, updated, ...rest} = { ...dataFromParent };
data = rest;
}