2

I have this object:

{
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
}

and another object is :

specificFeatures": {
    "id": "33343232",
    "createdAt": "2022-07-26T13:44:01.087Z",
    "updatedAt": "2022-07-26T13:45:31.000Z",
    "name": "Name Here",
    "description": "text",
    "coverage": "international",
    "income": 0,
    "observationIncome": "",
}

now, I want the property keys of the specificFeatures object that are the same as the first object to be deleted.

In this example, it would be:

specificFeatures": {
    "coverage": "international",
    "income": 0,
    "observationIncome": "",
}

Can you tell me how can I do this?

3 Answers 3

2

To achieve that, you can simply iterate over the object properties, check whether the "original" object has the property, and if so, delete it from "specificFeatures" object:

const original = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
}

const specificFeatures = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.087Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
  "coverage": "international",
  "income": 0,
  "observationIncome": "",
}

// Iterate over object keys
Object.keys(specificFeatures).forEach(prop => {
  // Check if the "original" object has the property defined
  if (original.hasOwnProperty(prop)) {
    // Delete the property from the "specificFeatures" object
    delete specificFeatures[prop];
  }
});

console.log(specificFeatures);

Pay attention that this way you mutate the original object, and in case you need to keep the original object as is, a new unique copy should be created. One of the possible solutions could be a combination of Object.entries() and Array.reduce() methods:

const original = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
}

const specificFeatures = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.087Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
  "coverage": "international",
  "income": 0,
  "observationIncome": "",
}

const result = Object.entries(specificFeatures).reduce((acc, [key, val]) => {
  if (!original.hasOwnProperty(key))
    acc[key] = val;
  return acc;
}, {});

console.log(result);
console.log('Is a different object:', result !== specificFeatures);

Sign up to request clarification or add additional context in comments.

Comments

1

This can be achieved in a simple one-liner:

var specificFeatures = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.087Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
  "coverage": "international",
  "income": 0,
  "observationIncome": "",
}

var myObj = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
}

const newObj = Object.fromEntries(Object.entries(specificFeatures).filter(([key]) => !(myObj.hasOwnProperty(key))));
console.log(newObj);

The above converts the object into an array of entries, then filters the keys against myObj before converting back into an Object.

Comments

0

The lodash function omit is a tidy way of doing this (specially if you're already using lodash)

const original = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
}

const specificFeatures = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.087Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
  "coverage": "international",
  "income": 0,
  "observationIncome": "",
}

const result = omit(original, Object.keys(specificFeatures))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.