I find myself presented with this pattern quite a bit. I have an array of objects that I get back from my api, and I need to manipulate just one of the properties in all of the objects.
Is there a way using ES6/Babel or Typescript to get that pattern to be a little more declarative?
Looking for some neat destructuring trick or something along those lines.
Object spread (...
), available in Babel using the Stage 3 preset, does the trick:
const data = [
{ foo: 1, bar: 2 },
{ foo: 2, bar: 3 },
{ foo: 3, bar: 4 },
];
const increment = a => a + 1;
const result = data.map(o => ({ ...o, foo: increment(o.foo) }));
console.log(result);