0% found this document useful (0 votes)
4 views

Javascript Iterators&Objects

Uploaded by

yliugbhr
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Javascript Iterators&Objects

Uploaded by

yliugbhr
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Iterators(Higher-Order Functions)

Objects, can be assigned to variables just using braces {}, each key-value pair is
separated with a comma
key-value pairs // An object literal with two key-value pairs, cat is the object
let cat = {
'Hated Enemies': ['Water', 'Dogs'], has quotation marks if it contains a
space character
tails: 1,
eat: 'Cat Food'
};

Delete and add poperty object


let color = {
red: 'Apple', green: 'Kiwi', yellow: 'Mango', }
delete color.yellow;
color.orange = 'Orange'; // red: 'Apple' green: 'Kiwi' orange: 'Orange'

Two ways to access an object's property (first way - dot notation, second way -
bracket notation[])
let spaceship = {
homePlanet: 'Earth',
'Fuel Type': 'Turbo Fuel',
numCrew: 5,
flightPath: ['Venus', 'Mars', 'Saturn']
};
spaceship.homePlanet; // returns 'Earth'
const myShip = 'numCrew'
const space = spaceship.galaxy; // returns undefined
const myCrew = spaceship[myShip]
console.log(spaceship.flightPath)); // output ['Venus', 'Mars', 'Saturn']
console.log(myCrew); // 5

Must use bracket notation when accessing keys that have numbers, spaces, or special
characters, without them the code
would throw an error
let spaceship = {
'Fuel Type' : 'Turbo Fuel',
'Active Mission' : true,
homePlanet : 'Earth',
numCrew: 5 <--- number: 13, will be added
};
spaceship['Fuel Type']; // returns 'Turbo Fuel'
spaceship['Milky Way']; // returns undefined
console.log(spaceship['Fuel Type']); // output Turbo Fuel
console.log(spaceship['Active Mission']); // output true

const article = {
"title": "How to create objects in JavaScript",
"link": "https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-
in-javascript-b0e2450655e8/",
"author": "Kaashan Hussain",
"language": "JavaScript",
"tags": "TECHNOLOGY",
"createdAt": "NOVEMBER 28, 2018"
};

const articleAuthor = article["author"]; // Kaashan Hussain


const articleLink = article["link"]; // https://www.freecodecamp.org/news/a-
complete-guide-to-creating-objects-in-javascript-b0e2450655e8/

const value = "title"; // "title"


const valueLookup = article[value]; // How to create objects in JavaScript

-Object inside a function


function phoneticLookup(val) {
var result = "";

var lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
};
result = lookup[val];
return result;
}
console.log(phoneticLookup("charlie")); // Chicago

Object inside an array


const myMusic = [
{
'artist': 'Bol4',
'title': 'Some',
'release_year': 2014,
'formats': ["CD", "8T", "Lp"]
}
];

Objects are mutable meaning we can update them after we created them using dot
notation or bracket notation
1. spaceship['Fuel Type'] = 'vegetable oil'; 2. spaceship.numCrew = 3;
-If there was no property with that name, a new property will be added to the
object
spaceship.number = 13;
-Delete a property from a object, delete spaceship.homePlanet; // removes the
home.Planet property
delete spaceship['Active Mission']; // removes the 'Active Mission' property

-Testing for properties on objects, .hasOwnProperty(property name)


const myCloth = { console.log(myCloth.hasOwnProperty('top'));
// true
top: 'shirt', bottom: 'pants' }; myCloth.hasOwnProperty('shoe'); // false

-Object that stored function data is called a method


const alienShip = { Object methods are invoked by
using the object
invade() { console.log('Conquer') }, name with dot operator followed
by method name
retreat() { console.log('Run') } and parentheses
}; alienShip.invade(); // Conquer

-Nested Objects
const myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
const gloveBoxContents = myStorage.car.inside['glove box']
console.log(gloveBoxContents) // maps

-Nested array with objects


const ourPets = [
{
animalType: "cat",
names: ["Meowzer", "Fluffy", "Kit-Cat"]
},
{
animalType: "dog",
names: ["Spot", "Bowser", "Frankie"]
}
], const myPets = ourPets[0].names[2]
console.log(myPets) // Kit-Cat

Pass by reference
!!!!!!!!!! let spaceship = {
'Fuel Type' : 'Turbo Fuel',
homePlanet : 'Earth'
};
let greenEnergy = obj => {
obj['Fuel Type'] = 'avocado oil';
}

let remotelyDisable = obj => {


obj.disabled = true;
}

greenEnergy(spaceship);
remotelyDisable(spaceship);
console.log(spaceship)

-Looping through objects


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!') }
} } };
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}`)
};
// captain: Lily, chief officer: Dan, medic: Clementine, translator: Shauna
Lily: Computer Engineering, Dan: Aerospace Engineering, Clementine: Physics,
Shauna: Conservation Science

You might also like