Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter by object key from array of object in js

I am trying to remove the Id from the array of objects by filtering but the id is not removed. I have used the filter method then I am filtering object keys.

const data= [{
    "id": 1,
    "group_name": "Science",
    "date": 2023,
    "created_table_at": "2022-08-20T01:22:40.000Z",
    "roll": "1401",
    "name": "Israt Jahan",

}]
const filteredData = data.filter((result) =>
          Object.keys(result)
            .filter((key) => key !== "id")
            .reduce((obj, key) => {
              obj[key] = result[key];
              return obj;
            }, {})
        );
console.log(filteredData)

My Expected output:

[{
        "group_name": "Science",
        "date": 2023,
        "created_table_at": "2022-08-20T01:22:40.000Z",
        "roll": "1401",
        "name": "Israt Jahan",

    }]
like image 685
Karim Avatar asked Oct 18 '25 20:10

Karim


1 Answers

You could try this solution:

const data = [{
  id: 1,
  group_name: 'Science',
  date: 2023,
  created_table_at: '2022-08-20T01:22:40.000Z',
  roll: '1401',
  name: 'Israt Jahan'
}];

/**
 * For each object in the data array, keep everything except the id.
 */
const filteredData = data.map(({ id, ...rest }) => rest);

console.log(filteredData);
like image 150
Alexandre Guertin Avatar answered Oct 21 '25 11:10

Alexandre Guertin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!