Reversing an Object.entries conversion

Sure, just use .reduce to assign to a new object:

const input = { key:'val', key2:'val2', key3:'val3' };

const output = Object.entries(input)
  .filter(([k, v]) => {
    return true; // some irrelevant conditions here
  })
  .reduce((accum, [k, v]) => {
    accum[k] = v;
    return accum;
  }, {});
console.log(output);

In modern browsers, you can also use Object.fromEntries which makes this even easier – you can just pass an array of entries, and it’ll create the object from those entries.

const input = { key:'val', key2:'val2', key3:'val3' };

const output = Object.fromEntries(
  Object.entries(input)
    .filter(([k, v]) => {
      return true; // some irrelevant conditions here
    })
);
console.log(output);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)