0

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)));

Console output: enter image description here

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:
enter image description here

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;
        }
knot22
  • 2,648
  • 5
  • 31
  • 51
  • Does this answer your question? [Vue: disable no-unused-vars error: the simplest fix](https://stackoverflow.com/questions/61874994/vue-disable-no-unused-vars-error-the-simplest-fix) – DecPK Apr 14 '21 at 12:01
  • Somewhat. Since there are no imports causing problems I had to figure out where to put that statement. I'll edit the original question to show what changes worked. – knot22 Apr 14 '21 at 12:07
  • Is it worked after adding the comment? – DecPK Apr 14 '21 at 12:11
  • 1
    Yes, that looked after the `"no-unused-vars"` errors. Hooray! – knot22 Apr 14 '21 at 12:13

2 Answers2

2

You can use spread to exclude properties.

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 { created, updated, ...data} = {...dataFromParent };
  
console.log(data);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1

You could use Object destructuring so that it will assign created, updated values, and rest other properties assign in a single object.

As you are using VUE, so add the following snippet before all import in script tag in x.vue file. SEE FOR VUE

// eslint-disable-next-line no-unused-vars

Disallow Unused Variables (no-unused-vars)

If error still persist then change the ESLint config in package.json

"rules": {
   "no-unused-vars": "off"
}

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 { created, updated, ...rest } = dataFromParent; // make a shallow copy to avoid changing original object values
const data = rest;

console.log(data);
DecPK
  • 24,537
  • 6
  • 26
  • 42