1

I have the following array of objects. I wanted to make new array of object excluding date field.

The new array should return as follows:

const new_arr = [
      {
        "id": 1,
        "user": 0
      },
      {
        "id": 2,
        "user": 0
       }
    ]

What's my mistake here?

const arr = [{
    "id": 1,
    "date": 0,
    "user": 0
  },
  {
    "id": 2,
    "date": 0,
    "user": 0
  }
]


const new_arr = arr.filter((items) => !items.date)

console.log(new_arr);
isherwood
  • 58,414
  • 16
  • 114
  • 157
Teshie Ethiopia
  • 586
  • 8
  • 27

1 Answers1

4

you can use map

like this

const arr = [
  {
    "id": 1,
    "date": 0,
    "user": 0
  },
  {
    "id": 2,
    "date": 0,
    "user": 0
   }
]

console.log(arr.map(({date, ...rest}) => ({...rest})))
R4ncid
  • 6,944
  • 1
  • 4
  • 18