JavaScript - How do I exclude elements from an object?

Viewed 2596

I have an object MainObject, but I would like to exclude and map some of the keys before returning it to the user. However, I do not know all of the keys since I use the ...{} operator to let the user add some custom data into the object.

My code looks something like this:

var UserData = {Id: 123, Name: "Hello World"}
var Data = {Main: "Data", Secret: "Data1", Math: 22, ...UserData}
// Resulting Object Keys [Main, Secret, Math, Id, Name]
// Wanted Object {Main, Math/2, Id, Name}
var result = ?

What I would like to do is to exclude the Secret property and devide the Math property by 2, while keeping the Data object intact.

5 Answers
const { Secret, ...result } = Data;

result.mathDivBy2 = function() { return this.Math/2; }

console.log(result);


To get Math/2 you call result.mathDivBy2();

If you want more convenient access, instead of a function, you can define getter for result object:

Object.defineProperty(result, 'mathDivBy2', {
  get: function() { return this.Math/2; } 
});

and use it like any other property: result.mathDivBy2

You could destructure your object as follows and assign Math / 2 after destructuring:

var UserData = { Id: 123, Name: "Hello World" };
var Data = { Main: "Data", Secret: "Data1", Math: 22, ...UserData };
var { Secret, ...result } = Data;
result.Math /= 2;
console.log(result);

  1. To remove the Secret key-value pair, all you need to do is to do delete Data.Secret.
  2. To divide Data.Math by 2, you can do Data.Math /= 2.

const UserData = {
  Id: 123,
  Name: "Hello World"
}
const Data = {
  Main: "Data",
  Secret: "Data1",
  Math: 22,
  ...UserData
}

// Delete `Data.Secret`
delete Data.Secret;

// Divide `Data.Math`
Data.Math /= 2;

console.log(Data);

However, if you are not too sure the shape of UserData coming in and want to make sure that only whitelisted key-value pairs are allowed, you will have to define the allowed keys and then iterate through your entire object to remove unwanted keys:

const UserData = {
  Id: 123,
  Name: "Hello World"
}
const Data = {
  Main: "Data",
  Secret: "Data1",
  Math: 22,
  ...UserData
}

// Whitelist allowed object keys
const allowedKeys = ['Main', 'Math', 'Id', 'Name'];
Object.keys(Data).forEach(key => {
  if (!allowedKeys.includes(key))
    delete Data[key];
});

// Divide `Data.Math`
Data.Math /= 2;

console.log(Data);

just delete it...

var UserData = {Id: 123, Name: "Hello World"}
var Data = {Main: "Data", Secret: "Data1", Math: 22, ...UserData}
var forexclude  = Object.assign({}, Data); //for cloning object
var excluded = delete forexclude.Secret; // for remove Secret

alert(JSON.stringify(forexclude, null, 4));
alert(JSON.stringify(excluded, null, 4));

// Resulting Object Keys [Main, Secret, Math, Id, Name]
// Wanted Object {Main, Math/2, Id, Name}
var result = ?

Since you want to keep the Data object intact. Its better to deep clone it first and then remove unwanted keys and update the value as required.

var UserData = {Id: 123, Name: "Hello World"}
var Data = {Main: "Data", Secret: "Data1", Math: 22, ...UserData};
var result = JSON.parse(JSON.stringify(Data));
delete result["Secret"];
result["Math"] /= 2;

console.log(result);
//Data will remains as it is
console.log(Data);

Related