简体   繁体   中英

How to remove object properties if exist on another object in Javascript / Typescript

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?

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

Here's one way you could do this using JavaScript:

    const firstObject = {
  "id": "33343232",
  "createdAt": "2022-07-26T13:44:01.080Z",
  "updatedAt": "2022-07-26T13:45:31.000Z",
  "name": "Name Here",
  "description": "text",
  "productType": "credit-card",
  "empresaId": null,
  "slug": null,
  "internalLink": "https://www.google.com/",
};

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

const filteredSpecificFeatures = {};

for (const key in specificFeatures) {
  if (!firstObject.hasOwnProperty(key)) {
    filteredSpecificFeatures[key] = specificFeatures[key];
  }
}

console.log(filteredSpecificFeatures);
// Output: { coverage: 'international', income: 0, observationIncome: '' }

This code first defines the two objects you provided. Then, it creates a new empty object called filteredSpecificFeatures .

It then iterates over the properties in the specificFeatures object using a for...in loop. For each property, it checks if the property exists on the firstObject object using the hasOwnProperty method. If the property doesn't exist on firstObject , it is added to the filteredSpecificFeatures object.

Finally, the code logs the filteredSpecificFeatures object to the console, which should output an object with only the properties that are unique to the specificFeatures object.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM