按对象移除对象内的对象 属性

Remove Object inside object by object property

我有一个这样的对象

{ 
  "6":{  
    "id":2045,
    "categories":[  
      {  
         "id":7,
         "name":"Day Trips & Excursions Outside the City"
      },
      {  
         "id":2,
         "name":"Day-Tour"
      },
      {  
         "id":8,
         "name":"Food, Wine & Gastronomy"
      }
   ],
},
"8":{  
   "id":2045,
   "categories":[  
      {  
         "id":7,
         "name":"Day Trips & Excursions Outside the City"
      },
      {  
         "id":2,
         "name":"Day-Tour"
      },
      {  
         "id":8,
         "name":"Food, Wine & Gastronomy"
      }
   ],
  },
},

现在我只想删除每个对象,如果它的 属性(id) 匹配用户提供的 id

   [2045, 1234]

所以在这种情况下,我想删除 id 为 2045 或 1234 的对象

 I tried multiple solution but failed, I just want to update the object something



   const categorytobedeleted = [2045, 12345];
   for(const v of categorytobedeleted) {
   const newobject = Object.values(oldobject).map(function(x) {
                  return x
              }).indexOf(v);
            Object.values(oldobject).slice(newobject, 1)
          }

     this.oldobject = this.newobject // maybe old object recreated with newly created object after deleting 

您可以将 reduce 用于此目的:

const newObject = Object.keys(oldobject).reduce((a, oldKey) => {
    if (!categorytobedeleted.includes(oldobject[oldKey].id)) {
        a[oldKey] = oldobject[oldKey]
    }
    return a
}, {})