Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Object.fromEntries?

I receive an object like this:

this.data = {
    O: {
        id: 0,
        name: value1,
        organization: organization1,
        ...,
       },
    1: {
        id: 1,
        name: value1,
        organization: organization1,
        ...,
        },
    2: {
        id: 2,
        name: value2,
        organization: organization2,
        ...,
        },
    ...
   } 

I then filter by id and remove the Object which id matches the id I receive from the store like so:

  filterOutDeleted(ids: any[], data: object,) {
    const remainingItems = Object.fromEntries(Object.entries(data)
      .filter(([, item]) => !ids.some(id => id === item.id)));

    const rows = Object.keys(remainingItems).map((item) => remainingItems[item]);
    return rows;
  }

Unfortunately, I'm getting an error when building stating Property 'fromEntries' does not exist on type 'ObjectConstructor' and I am unable to make changes in the tsconfig file at this point. Is there an alternative for fromEntries for this case? Any help is much appreciated!

like image 544
suuuriam Avatar asked Oct 17 '25 18:10

suuuriam


1 Answers

Create the object outside instead, and for every entry that passes the test, assign it to the object manually.

Also note that you can decrease the computational complexity by constructing a Set of the ids in advance:

const filterOutDeleted = (ids: any[], data: object) => {
  const idsSet = new Set(ids);
  const newObj = {};
  for (const [key, val] of Object.entries(data)) {
    if (!idsSet.has(val.id)) {
      newObj[key] = val;
    }
  }
  return newObj;
};
like image 76
CertainPerformance Avatar answered Oct 20 '25 08:10

CertainPerformance