// 3 persons///////////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
}
var b = {
name: "Petr",
surname: "Petrov",
}
var c = {
name: "Stepan",
surname: "Stepanov",
}
// different fields////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
// fields check////////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
if ('sex' in a) {
alert("found word in a!")
}
if ('fathername' in b) {
alert("found word in b!")
}
if (typeof { age: 33 } === 'object') { alert("found in c!") }
// array of persons////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
var persons = []
persons.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
})
console.log(persons)
// loop of persons//////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
var persons = []
persons.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
})
for (let i = 0; i < persons.length; i++) {
console.log(persons[i]);
}
// loop of name and surname//////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
var persons = []
persons.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
})
for (let i = 0; i < persons.length; i++) {
console.log(persons[i].name, persons[i].surname);
}
// loop of loop of values////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
var persons = []
persons.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
})
for (let i = 0; i < persons.length; i++) {
for (let values in persons[i]) { console.log(persons[i][values]); }
}
// fullName///////////////////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
sex: "male",
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
}
var c = {
name: "Stepan",
surname: "Stepanov",
age: 33,
}
var persons = []
persons.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
})
for (let i = 0; i < persons.length; i++) {
if (persons[i].name && persons[i].surname) {
persons[i].fullName = persons[i].name + " " + persons[i].surname;
}
else if (persons[i].fathername) { persons[i].fullName = persons[i].name + " " + persons[i].fathername + " " + persons[i].surname; }
console.log(persons[i].fullName)
}
// serialize//////////////////////////////////
JSON.stringify(persons)
// deserialize////////////////////////////////
persons.push(JSON.parse('{"name":"Steve","surname":"Jobs","sex":"male"}'))
console.log(persons)
// HTML///////////////////////////////////////
var str = "
\n"
for (let i = 0; i < persons.length; i++) {
console.log(persons[i]);
str += `${persons[i].name} | \n
${persons[i].surname} |
\n`
}
str += "
"
console.log(str)
document.write(str)
// HTML optional fields///////////////////////
var a = {
name: "Ivan",
surname: "Ivanov",
fathername: "Ivanovich",
sex: "male",
age: 33,
}
var b = {
name: "Petr",
surname: "Petrov",
fathername: "Petrovich",
sex: "male",
age: 33,
}
var c = {
name: "Stepan",
surname: "Stepanov",
fathername: "Stepanovich",
sex: "male",
age: 33,
}
var persons1 = []
persons1.push(a, b, c, {
name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich", sex: "male", age: 33,
})
for (let i = 0; i < persons1.length; i++) {
console.log(persons1[i]);
}
var str = ""
for (let i = 0; i < persons1.length; i++) {
console.log(persons1[i]);
str += `${persons1[i].name} | ${persons1[i].surname} |
${persons1[i].fathername} | ${persons1[i].age} | ${persons1[i].sex} |
`
}
str += "
"
console.log(str)
document.write(str)
// HTML tr color + HTML th optional////////////
let keys = persons1.reduce((container, obj) => [...container, ...Object.keys(obj)], []);
let uniqueKeys = [...new Set(keys)];
var str = ""
for (let x = 0; x < uniqueKeys.length; x++) {
str += `${uniqueKeys[x]} | `
}
for (let i = 0; i < persons1.length; i++) {
if (i % 2 === 0) { str += `` }
else { str += '
' }
for (let j = 0; j < uniqueKeys.length; j++) {
if (Object.keys(persons1[i]).includes([...uniqueKeys][j])) {
str += `${persons1[i][[...uniqueKeys][j]]} | `
}
else {
str += `=== | `
}
}
str += '
'
}
str += "
"
console.log(str)
document.write(str)
// Задание на синий пояс.
// Сделать HTML-конструктор из деревянной структуры, которая была на прошлом занятии:
var someTree = {
tagName: "table", //html tag
subTags: [ //вложенные тэги
{
tagName: "tr",
subTags: [
{
tagName: "td",
text: "some text",
},
{
tagName: "td",
text: "some text 2",
}
]
}
],
attrs:
{
border: 1,
},
}
let table = `<${someTree.tagName} `
for (const attr in someTree.attrs) {
table += `${attr} = ${someTree.attrs[attr]}>`
}
for (let subTag of someTree.subTags) {
table += `<${subTag.tagName}>`
for (let innerTag of subTag.subTags) {
table += `<${innerTag.tagName}> ${innerTag.text}${innerTag.tagName}>`
}
table += `${subTag.tagName}>`
}
table += `${someTree.tagName}>`
console.log(table)
document.write(table)
// destruct array////////////////////////////
let arr = [1, 2, 3, 4, 5, "a", "b", "c"]
let [odd1, even1, odd2, even2, odd3, ...arr1] = arr
console.log(even1, even2);
console.log(odd1, odd2, odd3);
console.log(arr1);
// destruct string///////////////////////////
let arr2 = [1, "abc"]
let [number, [s1, s2, s3]] = arr2
// let number = arr2[0];
// let s1 = arr2[1][0];
// let s2 = arr2[1][1];
// let s3 = arr2[1][2];
console.log(number);
console.log(s1, s2, s3);
// destruct 2///////////////////////////////
let obj = {
name: 'Ivan',
surname: 'Petrov', children: [{ name: 'Maria' }, { name: 'Nikolay' }]
}
let { children: [{ name: name1 }, { name: name2 }] } = obj
console.log(name1, name2)
// let { name: name1 } = obj.children[0]
// let { name: name2 } = obj.children[1]
// destruct 3///////////////////////////////
let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];
let { 0: a, 1: b, length } = arr3
console.log(a, b, length)
// let [a, b] = arr3
// let length = arr3.length;