DEV Community

Discussion on: Mapping JavaScript Objects with Modern APIs

Collapse
 
pallymore profile image
Yurui Zhang • Edited

Neat.

However one thing to note is that Object.entries ignores all symbol keys. If we want to support symbols:

const mapValues = (input, mapper) =>
  Object.fromEntries(
    [...Object.getOwnPropertyNames(input), ...Object.getOwnPropertySymbols(input)].map(key => [
        key,
        mapper(input[key], key, input)
    ])
  );

=>

const input = {
  foo: 1,
  [Symbol('bar')]: 4,
};

const output = mapValues(input, v => v * 2);

expect(output).toEqual({
  foo: 2,
  [Symbol('bar')]: 8,
});