how to avoid specific key during spread operation

When using the spread operator in JavaScript, it is possible to avoid including a specific key by using the omit method from the Lodash library.

Here’s an example:

const originalObject = { a: 1, b: 2, c: 3 };
const { b, ...newObject } = { ...originalObject };

console.log(newObject); // { a: 1, c: 3 }

In this example, we first create an object originalObject with three keys (a, b, and c). Then, we use the spread operator (...) to create a new object newObject that includes all the keys from originalObject except for b.

By using the omit method from Lodash, we can achieve the same result:

const originalObject = { a: 1, b: 2, c: 3 };
const newObject = _.omit(originalObject, ['b']);

console.log(newObject); // { a: 1, c: 3 }

In this example, we pass originalObject and an array of keys to omit (['b']) to the _.omit method. The method returns a new object that includes all the keys from originalObject except for b.

For example

prevData[index]._id = prevData[index]?.file_id;
const {
_id,
isDelete,
createdAt,
createdBy,
updatedAt, // these key avoiding in newObject
updatedBy,
...newObject
}: any = { ...prevData[index] };

props.onUploadDocs(props?.accordionHeading, [newObject]);
setFileData(prevData);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top