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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up