szymcio32
10/26/2019 - 1:52 PM

Spread operators and rest parameters

// ...object is similar to *args

// creating new array
const posts = [
  { title: 'Post One', body: 'This is post one', authors: 1},
  { title: 'Post Two', body: 'This is post two', authors: 3 }
];

const newPosts = [...posts, ...[{title: 'Post Three'}]];


// Creating new object with data from posts[0] and new description
const newBody = {
    body: "This is edited body",
}
const newSinglePost = {
    ...posts[0],
    ...newBody,
}
// output: {title: "Post One", body: "This is edited body", authors: 1}


// Remove body from object
const withoutBody = ( {body, ...rest} ) => rest;