Skip to content

Instantly share code, notes, and snippets.

View darleykrefta's full-sized avatar
🎯
Focusing

Darley Krefta darleykrefta

🎯
Focusing
View GitHub Profile
const droid = {
  name: 'R2-D2',
  height: '1.08',
  colors: ['blue', 'silver']
}

// newDroid is a new variable created using rest operator
const { colors, ...newDroid } = droid
const droid = {
  name: 'R2-D2',
  height: '1.08',
  colors: ['blue', 'silver']
}

const newDroid = droid

delete newDroid.colors
const droid = {
  name: 'R2-D2',
  height: '1.08',
  colors: ['blue', 'silver']
}

const newDroid = {
  ...droid,
 colors: [...droid.colors, 'white']
const droid = {
  name: 'R2-D2',
  height: '1.08',
  colors: ['blue', 'silver']
}

const newDroid = { ...droid }

newDroid.colors.push('white')
const droid = {
  name: 'R2-D2',
  height: '1.08'
}

// insted of define a new object with reference, we pass a spread operator of the original object
const newDroid = {
  ...droid,
 colors: ['blue', 'silver', 'white'],
const droid = {
  name: 'R2-D2',
  height: '1.08'
}

// define newDroid with droid reference
const newDroid = droid

// add some new attributes
const jedis = ['Yoda', 'Luke Skywalker', 'Qui-Gon Jinn', 'Rey']

// define jedisSorted with a spread of jedis, creating a new sorted reference
const jedisSorted = [...jedis].sort()

// the original variable wasn't updated
console.log(jedis)
// ['Luke Skywalker', 'Qui-Gon Jinn', 'Rey', 'Yoda']
const jedis = ['Yoda', 'Luke Skywalker', 'Qui-Gon Jinn', 'Rey']

// define jedisSorted with jedis sorted reference
const jedisSorted = jedis.sort()

// the original variable is updated too, because it is the original reference
console.log(jedis)
// ['Luke Skywalker', 'Qui-Gon Jinn', 'Rey', 'Yoda']
const lightSide = ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']

// remove the first element using slice method, creating a new reference
// in this example we don't need to use spread operator
const newLightSide = lightSide.slice(1)

// the original variable wasn't updated
console.log(lightSide)
// ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']
const lightSide = ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']

// define newLightSide with lightSide reference
const newLightSide = lightSide

// remove the first element of the new array created
newLightSide.shift()

// the original variable is updated too, because it is the original reference