第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從嵌套的對象數(shù)組中刪除對象屬性?

如何從嵌套的對象數(shù)組中刪除對象屬性?

慕俠2389804 2023-05-19 16:13:49
我試圖在數(shù)組不工作時從數(shù)組中的每個對象中刪除這兩個屬性。每個對象都在一個數(shù)組中。我的意思是主數(shù)組包含對象數(shù)組。有沒有不使用 map() 兩次的簡單方法來解決它?在這種情況下如何返回修改后的主數(shù)組?const modifiedItems = this.items.map(item => {   delete item.created,   delete item.removed   return item})數(shù)據(jù)如下所示:this.items = [  [{ removed: 1, created: 1, a:2, b: 2, c: 3}]  [{ removed: 1, created: 1, a:2, b: 2, c: 3}, { removed: 1, created: 1, a:2, b: 2, c: 3},]];上面的代碼不起作用,因為它沒有映射到主數(shù)組中的數(shù)組。如何最好地從位于主數(shù)組中的所有數(shù)組的每個對象中刪除這些屬性?
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經(jīng)驗 獲得超7個贊

與其刪除,不如返回一個沒有要刪除的屬性的對象。

您可以解構要刪除的屬性,然后使用剩余參數(shù)語法將其他屬性收集到變量中。在此之后,您只需要返回包含除您要刪除的屬性之外的所有屬性的變量。

const modifiedItems = this.items.map(

? ?({ created, removed, ...rest }) => rest

);

以下代碼片段顯示了一個示例:


const arr = [

? { removed: 1, created: 1, a:2, b: 2, c: 3},

? { removed: 1, created: 1, a:2, b: 2, c: 3},

? { removed: 1, created: 1, a:2, b: 2, c: 3},

];


const modifiedItems = arr.map(

? ?({ created, removed, ...rest }) => rest

);


console.log(modifiedItems);

編輯:

在您的例子中,this.items是一個包含嵌套數(shù)組的數(shù)組。因此,要從嵌套數(shù)組內的對象中刪除屬性,您還需要映射每個嵌套數(shù)組。

以下代碼片段顯示了一個示例:

const items = [

? [ { removed: 1, created: 1, a: 2, b: 2, c: 3 } ],

? [

? ? { removed: 1, created: 1, a: 2, b: 2, c: 3 },

? ? { removed: 1, created: 1, a: 2, b: 2, c: 3}

? ]

];


const modifiedItems = items.map(subArr => {

? return subArr.map(({ created, removed, ...rest }) => rest)

});


console.log(modifiedItems);


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號