亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

通過刪除屬性但保留其內容來轉換對象

通過刪除屬性但保留其內容來轉換對象

www說 2022-09-29 15:37:01
我試圖找到一種方法來“減少”一個對象。這就是我所擁有的{ _attributes: { name: 'titolo', description: 'il titolo della sezione' },  field:   [ { _attributes:        { name: 'titolo',          type: 'input',          label: 'titolo',          value: 'titolo' } },     { _attributes:        { name: 'colore',          type: 'input',          select: 'giallo,blu',          label: 'seleziona il colore',          value: 'titolo' } }    ] }這就是我想要的{  name: 'titolo', description: 'il titolo della sezione' ,  field:   [         { name: 'titolo',          type: 'input',          label: 'titolo',          value: 'titolo' } ,        { name: 'colore',          type: 'input',          select: 'giallo,blu',          label: 'seleziona il colore',          value: 'titolo' }     ] }基本上刪除_attributes屬性,但保留其內容。我想知道除了循環對象之外,是否有其他智能方法。
查看完整描述

2 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

let obj = {

  _attributes: {

    name: 'titolo',

    description: 'il titolo della sezione'

  },

  field: [{

      _attributes: {

        name: 'titolo',

        type: 'input',

        label: 'titolo',

        value: 'titolo'

      }

    },

    {

      _attributes: {

        name: 'colore',

        type: 'input',

        select: 'giallo,blu',

        label: 'seleziona il colore',

        value: 'titolo'

      }

    }

  ]

}


obj = { ...obj._attributes, ...obj };

delete obj._attributes;

obj.field = obj.field.map(el => el._attributes);


console.log(obj);


查看完整回答
反對 回復 2022-09-29
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

如何做到這一點:reduce


var obj={ _attributes: { name: 'titolo', description: 'il titolo della sezione' }, field: [ { _attributes: { name: 'titolo', type: 'input', label: 'titolo', value: 'titolo' } }, { _attributes: { name: 'colore', type: 'input', select: 'giallo,blu', label: 'seleziona il colore', value: 'titolo' } } ] };


var result = Object.entries(obj).reduce((acc,[k,v])=>{

  if(!Array.isArray(v)){

     acc = {...v, ...acc};

    } else {

     field = v.map(({_attributes})=>_attributes);

     acc = {...acc, field}

    }

  return acc;

},{});


console.log(result);


查看完整回答
反對 回復 2022-09-29
  • 2 回答
  • 0 關注
  • 74 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號