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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果匹配條件,則在對(duì)象數(shù)組中添加鍵

如果匹配條件,則在對(duì)象數(shù)組中添加鍵

慕后森 2023-05-19 17:01:21
我正在嘗試根據(jù)條件獲取一組對(duì)象。如果該值為真,則僅將該鍵和值添加到新的對(duì)象數(shù)組中。但是,它給出了錯(cuò)誤:  const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]const newArr = arr.map(item => ({id: item.id, val: item.val != null ? item.val : ''}))console.log(newArr); //It is workingconst arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]const expectedArr = arr.map(item => ({id: item.id, val: item.val != null ? item.val : '', (item.show) && (show: item.show)}))console.log(expectedArr); //Shows error預(yù)期結(jié)果:[  {    "id": 123,    "val": "abcd"  },  {    "id": 123,    "val": "abcd"    "show": true  },  {    "id": 123,    "val": "abcd"  },  {    "id": 123,    "val": "abcd"  }]任何幫助將不勝感激。
查看完整描述

4 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

那 ?


const arr = 

      [ { id: 123, val: 'abcd', other: 'abcd' } 

      , { id: 123, val: 'abcd', other: 'abcd', show: true  } 

      , { id: 123, val: 'abcd', other: 'abcd', show: false } 

      , { id: 123, val: 'abcd', other: 'abcd' } 

      ] 


const newArr = arr.map(({id,val,show})=>show?{id,val,show}:{id,val})


console.log( newArr )

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對(duì) 回復(fù) 2023-05-19
?
拉丁的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

你可以像這樣重寫它。使用這種語(yǔ)法,當(dāng)item.show括號(hào)為真時(shí),您將解析為和對(duì)象,然后您只需將其傳播到您的對(duì)象中。您還可以使用無(wú)效合并來(lái)分配item.val


 const expectedArr = arr.map((item) => ({

      id: item.id,

      val: item.val ?? "",

      ...(item.show && { show: item.show }),

    }));


查看完整回答
反對(duì) 回復(fù) 2023-05-19
?
冉冉說(shuō)

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

目前,它在函數(shù)內(nèi)部的一行中包含多個(gè)條件,Array.map并且無(wú)效。


const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]


const result = arr.map(({ id, val, show }) => {

  const newObj = {

    id,

    val: val != null ? val : ''

  };

  if (show) {

    newObj['show'] = show;

  }

  return newObj;

});


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2023-05-19
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]


const expectedArr = arr.map(item => ({id: item.id, val: item.val? item.val: '', show: item.show? item.show: 0}))


console.log(expectedArr); //solved-ish


//but honestly, that's the furthest i can go with your strange code golfed syntax setup.. to be honest, using ? is similar to if but NOT if, it's closer to || but anyway


console.log('skip what was above, the moment of truth below')


const theRealAnswer = arr.map(item=>{ const i={id: item.id, val: item.val? item.val: '', show: item.show? item.show: 0};if(!i.show){delete(i.show)}return(i) })


console.log(theRealAnswer)


//now, the logical construct(what u gave the mapper) was impossible to do what u wanted. coding isn't just syntax, it's logic too. you cannot assign a key to completely delete the key itself


查看完整回答
反對(duì) 回復(fù) 2023-05-19
  • 4 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)