Reversing an Object.entries conversion

I am using Object.entries in order to get some values out of a nested object and filter it.

obj = Object.entries(obj)
  .filter(([k, v]) => {
    return true; // some irrelevant conditions here
  });

My object ends up as an array of arrays, of keys and vals.

[['key1', val]['key2', val']['key3', val]]

Is there a straightforward way to map these back into an object? The original object structure is:

{ key:val, key2:val2, key3:val3 }

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);

For new browsers, use Object.fromEntries:

Object.fromEntries(arr); 

For older js, it can still be a one liner.

arr.reduce((acc,[k,v])=>(acc[k]=v,acc),{})

Example:

Object.entries(sampleObject) // Turn object to array
   .reduce((acc,[k,v])=>(acc[k]=v,acc),{}) // Turn it back to object.

Using Object.assign with a map that maps [k,v] => {[k]: v}

For example, the code below will only keep keys beginning with key

var obj = {
  key: 1,
  key2: 2,
  key3: 3,
  removed: 4,
  alsoRemoved: 5
}

obj = Object.assign({}, ...Object.entries(obj)
    .filter(([k, v]) => {
        return k.startsWith('key');
    })
    .map(([k, v]) => ({[k]: v}))
);

console.log(obj);

Using reduce with deconstruction and comma operator:

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

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

which should give the same functionality as CertainPerformance’s answer with a bit more concise syntax

Read More:   Autosave input box's to database during pause in typing?

let entries = Object.entries({e: 'e', q: 'q'});
let reverse = entries.map(([t, r]) => ({[t]: r})).reduce((pv, cv) =>{return Object.assign(pv, cv)});
console.log(reverse);

If you know exactly which entries you want to exclude, you can use object deconstruction combined with spreading:

function clean(obj) {
  const { unwanted1, unwanted2, ...wanted } = obj;
  return { ...wanted };
}

For some cases, this might be the cleanest solution.

function undoEntries(entered){
  let output = {};
  entered.forEach(item => {
    output[item[0]] = item[1]
  });
  return output;
};
// Example
const obj = { a: 1, b: 2, c: 3};
const input = Object.entries(obj);
const output = undoEntries(input);
console.log(output);


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts