Snippets Collections
const todo = {
  id: 1,
  firstName: "David",
  lastName: "Mchale",
  age: 40,
  user: {
    isLoggedIn: false,
  },
};


// using curly braces means we are destructuring
// NOT using curly braces and using ':' means we are just renaming

const {
  id: position, // renaming
  firstName,
  lastName,
  age,
  user: { isLoggedIn }, // getting the value from the user isLoggedIn
} = todo;
console.log("destructure => ", firstName, lastName, age, isLoggedIn, position);
const todos = [
  {
    id: 1,
    task: "buy milk",
  },
  {
    id: 2,
    task: "go to gym",
  },
  {
    id: 3,
    task: "clean apartment",
  },
];

const existingToDos = []; // create new array
todos.forEach((item) => todos.length && existingToDos.push(item)); // loop over and push into new arrat if the length is more than 0;

console.log("existing =>", existingToDos);





// check todos that have a task that has a string containing 'buy'
const containsBuy = [];
const reguarExp = new RegExp("buy");
todos.filter((item) => item.task.match(reguarExp) && containsBuy.push(item));
console.log("containsBuy => ", containsBuy);
const defaultOptions = {
  verbose: false,
  src: "./src",
  dist: "./dist"
};
const userOptions = { verbose: true };

const options = { ...defaultOptions, ...userOptions };
function returnDayKeyValue(val) {
    const returnDayObject = {
        1: "It's monday",
        2: "It's tuesday",
        3: "It's wednesday",
        4: "It's thursday",
        5: "It's friday",
        6: "It's saturday",
        7: "It's sunday",
    }
    if(!returnDayObject[val]){
     return "Enter a value between 1 - 7";
    }
    return returnDayObject[val]
}
cosnt day = 3;
console.log(returnDayKeyValue(day)); //It's wednesday
const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  _numOfSensors: 15,
  get numOfSensors(){
    if(typeof this._numOfSensors === 'number'){
      return this._numOfSensors;
    } else {
      return 'Sensors are currently down.'
    }
  },
  set numOfSensors(num) {
    if (typeof num === 'number' && num >= 0){
      this._numOfSensors = num;
    } else {
      console.log('Pass in a number that is greater than or equal to 0')
    }   
  } 
};

robot.numOfSensors = 100;
console.log(robot.numOfSensors);
const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel() {
    if (typeof this._energyLevel === 'number') {
      return `My current energy level is ${this._energyLevel}`
    } else {
      return 'System malfunction: cannot retrieve energy level'
    }
  }
};

console.log(robot.energyLevel)
let spaceship = {
    crew: {
    captain: { 
        name: 'Lily', 
        degree: 'Computer Engineering', 
        cheerTeam() { console.log('You got this!') } 
        },
    'chief officer': { 
        name: 'Dan', 
        degree: 'Aerospace Engineering', 
        agree() { console.log('I agree, captain!') } 
        },
    medic: { 
        name: 'Clementine', 
        degree: 'Physics', 
        announce() { console.log(`Jets on!`) } },
    translator: {
        name: 'Shauna', 
        degree: 'Conservation Science', 
        powerFuel() { console.log('The tank is full!') } 
        }
    }
}; 

// Write your code below

for (let crewMember in spaceship.crew) {
  console.log(`${crewMember}: ${spaceship.crew[crewMember].name}`)
};

for (let crewMember in spaceship.crew) {
  console.log(`${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}`)
};
let spaceship = {
  passengers: null,
  telescope: {
    yearBuilt: 2018,
    model: "91031-XLT",
    focalLength: 2032 
  },
  crew: {
    captain: { 
      name: 'Sandra', 
      degree: 'Computer Engineering', 
      encourageTeam() { console.log('We got this!') },
     'favorite foods': ['cookies', 'cakes', 'candy', 'spinach'] }
  },
  engine: {
    model: "Nimbus2000"
  },
  nanoelectronics: {
    computer: {
      terabytes: 100,
      monitors: "HD"
    },
    'back-up': {
      battery: "Lithium",
      terabytes: 50
    }
  }
}; 

let capFave = spaceship.crew.captain['favorite foods'][0]

spaceship.passengers = [{name: 'Space Dog'}]

let firstPassenger = spaceship.passengers[0]
let cars = [
  {
    "color": "purple",
    "type": "minivan",
    "registration": new Date('2017-01-03'),
    "capacity": 7
  },
  {
    "color": "red",
    "type": "station wagon",
    "registration": new Date('2018-03-03'),
    "capacity": 5
  },
  {
    ...
  },
  ...
]

// add to the beginning 
const car = {
  "color": "red",
  "type": "cabrio",
  "registration": new Date('2016-05-02'),
  "capacity": 2
}
cars.unshift(car);

// add to the end
const car = {
 "color": "red",
 "type": "cabrio",
 "registration": new Date('2016-05-02'),
 "capacity": 2
}
cars.push(car);

// add somewhere in the middle 
Array.splice(
  {index where to start},
  {how many items to remove},
  {items to add}
);

// place the car after the 4th element in the array
let car = {
  "color": "red",
  "type": "cabrio",
  "registration": new Date('2016-05-02'),
  "capacity": 2
}
cars.splice(4, 0, car);
function merge(...objects) {
  let masterObj = {}

  // iterate over `objects` merging each
  // into `masterObj` to generate flattened
  // object
  for (let i = 0; i < objects.length; i++) {
    let obj = objects[i]
    for (let key in obj) masterObj[key] = obj[key]
  }

  return masterObj
}

let merged = merge(...objectsList)

// output:
// {count:5, delay:2000, early:false, message:'Hello'}
console.log(merged)
var eventsVariable = '"{events":[' +
    '{"location": "New York", "date": "May 1", "public": "true"},' +
    '{"location": "London", "date": "Apr 24", "public": "false"},' +
    '{"location": "San Frans", "date": "Nov 30", "public": "false"}]}';
    
star

Sun Apr 28 2024 01:09:48 GMT+0000 (Coordinated Universal Time)

#objects #destructuring
star

Sun Apr 28 2024 00:11:22 GMT+0000 (Coordinated Universal Time)

#objects #checking #array #regex
star

Wed Apr 05 2023 16:06:24 GMT+0000 (Coordinated Universal Time) https://codetogo.io/how-to-concatenate-objects-in-javascript/

#javascript #objects #join
star

Wed Dec 01 2021 17:02:56 GMT+0000 (Coordinated Universal Time) https://medium.com/@abhinav_vp/another-4-javascript-tips-for-shorter-code-64791c1111b4

#objects #javascript
star

Tue Oct 05 2021 16:17:03 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/setters

#javascript #objects #advancedobjects #setters #if
star

Tue Oct 05 2021 16:09:40 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/getters

#javascript #objects #getters #advancedobjects
star

Tue Oct 05 2021 15:25:10 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/for-in

#javascript #objects
star

Tue Oct 05 2021 14:55:28 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/nested-objects

#javascript #objects
star

Thu Jun 17 2021 11:00:23 GMT+0000 (Coordinated Universal Time)

#javascript #objects #arrays
star

Mon Mar 01 2021 03:26:59 GMT+0000 (Coordinated Universal Time) https://www.benmvp.com/blog/learning-es6-parameter-handling/#spread-operator

#javascript #objects

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension