DEV Community

Sam Clark
Sam Clark

Posted on

2 2

Horrible Snippet - Updating an Nested Array With The Spread Operator

Sometimes I like to just sit there and think of weird ways to accomplish things, and sometimes I come up with ideas like this, I think this could be applied to some Functional JS

// I Really dig this one
const foo = {
  bar: {
    baz: [{ changeMe: 'asdf', notMe: 'asdf' }, { changeMe: 'ffff', notMe: 'asdf' }],
  },
};

const newArr = foo.bar.baz.map(baz => ({ ...baz, changeMe: 'swee' }));

const newData = {
  ...foo,
  bar: {
    baz: newArr,
  },
};

console.log('newData', newData.bar.baz);

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more