为了账号安全,请及时绑定邮箱和手机立即绑定

如果匹配条件,则在对象数组中添加键

如果匹配条件,则在对象数组中添加键

慕后森 2023-05-19 17:01:21
我正在尝试根据条件获取一组对象。如果该值为真,则仅将该键和值添加到新的对象数组中。但是,它给出了错误:  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预期结果:[  {    "id": 123,    "val": "abcd"  },  {    "id": 123,    "val": "abcd"    "show": true  },  {    "id": 123,    "val": "abcd"  },  {    "id": 123,    "val": "abcd"  }]任何帮助将不胜感激。
查看完整描述

4 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

那 ?


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


查看完整回答
反对 回复 2023-05-19
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

你可以像这样重写它。使用这种语法,当item.show括号为真时,您将解析为和对象,然后您只需将其传播到您的对象中。您还可以使用无效合并来分配item.val


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

      id: item.id,

      val: item.val ?? "",

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

    }));


查看完整回答
反对 回复 2023-05-19
?
冉冉说

TA贡献1877条经验 获得超1个赞

目前,它在函数内部的一行中包含多个条件,Array.map并且无效。


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


查看完整回答
反对 回复 2023-05-19
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

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


查看完整回答
反对 回复 2023-05-19
  • 4 回答
  • 0 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信