Add key in Array of object if match condition

I am trying to get an array of objects based on a condition. If the value is true then only add that key and value to new array of object.

But, It gives error:

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 working

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 != null ? item.val : '', (item.show) && (show: item.show)}))

console.log(expectedArr); //Shows error

Expected Result:

[
  {
    "id": 123,
    "val": "abcd"
  },
  {
    "id": 123,
    "val": "abcd"
    "show": true
  },
  {
    "id": 123,
    "val": "abcd"
  },
  {
    "id": 123,
    "val": "abcd"
  }
]

Any help would be greatly appreciated.

Answer

that ?

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