JS Sticky Notes: Spread Operator


Part of the JS Sticky Notes series: Spread operator

Conditional JSONs

var obj = {
  title: "hello"
};

var upperCase = true;

var newObj = {
  ...obj,
  ...(upperCase && {
    title: "HELLO"
  })
};

Extract from JSONs

var item = {
  id: "1",
  title: "Something",
  price: "100"
};

const { id, ...itemDetails } = item;

//id -> 1
// itemDetails -> {title:"Something", price:"100"}

Use forEach with any iterable

var map = new Map();

[...map].forEach(() => {});