123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- // 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 = "<table border='1'>\n"
- for (let i = 0; i < persons.length; i++) {
- console.log(persons[i]);
- str += `<tr><td>${persons[i].name}</td>\n
- <td>${persons[i].surname}</td></tr>\n`
- }
- str += "</table>"
- 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 = "<table border='1'>"
- for (let i = 0; i < persons1.length; i++) {
- console.log(persons1[i]);
- str += `<tr><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
- <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
- }
- str += "</table>"
- console.log(str)
- document.write(str)
- // HTML tr color//////////////////////////////
- var str = "<table border='1'>"
- for (let i = 0; i < persons1.length; i++) {
- console.log(persons1[i]);
- if (i % 2 === 0) {
- str += `<tr style="background-color:maroon"><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
- <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
- }
- else {
- str += `<tr><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
- <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
- }
- }
- str += "</table>"
- console.log(str)
- document.write(str)
- // HTML th optional///////////////////////////
|