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.
(item.show) && (show: item.show)to...(item.show && {show: item.show})