112 lines
2.4 KiB
JavaScript
112 lines
2.4 KiB
JavaScript
//DESTRUCTING
|
|
|
|
const colors = ["red", "blue", "green", "black", "white"];
|
|
console.log(colors);
|
|
|
|
//swap
|
|
[colors[0], colors[2]] = [colors[2], colors[0]];
|
|
console.log(colors);
|
|
|
|
const [firstn, secondn, thirdn, ...restn] = colors;
|
|
|
|
console.log(firstn);
|
|
console.log(secondn);
|
|
console.log(thirdn);
|
|
console.log(restn);
|
|
|
|
const person1 = {
|
|
first : "Sponge",
|
|
last : "Squarepants",
|
|
age : 30,
|
|
job : "FryCook"
|
|
};
|
|
const person2 = {
|
|
first : "Patrick",
|
|
last : "Star",
|
|
age : 35,
|
|
};
|
|
|
|
const {first, last, age, job = "Unemployed"} = person2;
|
|
console.log(first);
|
|
console.log(last);
|
|
console.log(age);
|
|
console.log(job);
|
|
|
|
function DisplayPerson({first, last, age, job = "Unemployed"}){
|
|
console.log(`${first} ${last} age: ${age} is currently ${job}`);
|
|
}
|
|
|
|
DisplayPerson(person1);
|
|
DisplayPerson(person2);
|
|
|
|
|
|
|
|
//NESTED OBJECTS
|
|
|
|
const persona = {
|
|
fullName : "Spongebob Squarepants",
|
|
age : 30,
|
|
isStudent : true,
|
|
hobbies : ["Karate", "Danceing", "Cooking"],
|
|
address : {
|
|
street : "Some st",
|
|
city : "Bikkini bottom",
|
|
country : "In Water"
|
|
}
|
|
}
|
|
|
|
console.log(persona.fullName);
|
|
console.log(persona.age);
|
|
console.log(persona.isStudent);
|
|
console.log(persona.hobbies[0]);
|
|
console.log(persona.hobbies[1]);
|
|
console.log(persona.hobbies[2]);
|
|
console.log(persona.address.city);
|
|
|
|
persona.hobbies.forEach(element => {
|
|
console.log(element)
|
|
});
|
|
|
|
for(const item in persona.address){
|
|
console.log(persona.address[item])
|
|
}
|
|
|
|
class Person{
|
|
constructor(name, age, ...adres){
|
|
this.name = name;
|
|
this.age = age;
|
|
this.address = new Address(...adres);
|
|
}
|
|
}
|
|
class Address{
|
|
constructor(street, city, country){
|
|
this.street = street;
|
|
this.city = city;
|
|
this.country = country;
|
|
}
|
|
}
|
|
|
|
const p = new Person("Spongebob", 32, "sth st.", "Bikini bottm", "In tha ater");
|
|
console.log(p)
|
|
|
|
|
|
//SORTING
|
|
|
|
colors.sort();
|
|
console.log(colors)
|
|
|
|
const nums = [1,2,6,8,4,41,2,5,6,10,2,0,1,2,5,7,8,9,3,3,2,5,4,25,74,85,25,36,66,41,23,36]
|
|
console.log(nums.sort())
|
|
|
|
console.log(nums.sort((a, b) => a - b))
|
|
console.log(nums.sort((a, b) => b - a))
|
|
|
|
const people = [{name: "Sponge", age: 25, gpa: 5.2},
|
|
{name: "Patrick", age: 35, gpa: 3.5},
|
|
{name: "Sandy", age: 32, gpa: 4.7},
|
|
{name: "Sqidwards", age: 32, gpa: 14.7}];
|
|
|
|
console.log(people.sort((a, b) => a.age - b.age))
|
|
console.log(people.sort((a, b) => a.gpa - b.gpa))
|
|
console.log(people.sort().name)
|