123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // 3 persons
- let a = {
- name: "Andrey",
- surname: "Ivanov",
- }
- let b = {
- name: "Igor",
- surname: "Vasin",
- }
- let c = {
- name: "Oleg",
- surname: "Petrov",
- }
- //different fields
- let a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- }
- let b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- }
- let c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- }
- //fields check
- let a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- }
- let b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- }
- let c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- }
- if ("sex" in a) {
- alert(a.sex)
- } if ("age" in a) {
- alert(a.age)
- } if ("sex" in b) {
- alert(b.sex)
- } if ("age" in b) {
- alert(b.age)
- } if ("sex" in c) {
- alert(c.sex)
- } if ("age" in c) {
- alert(c.age)
- }
- //array of persons
- let arraypersons = [a, b, c, { name: 'Daniil', surname: 'Kovalev', fathername: "Alexandrovich", age: 30 }]
- //loop of persons
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- }
- ]
- for (i = 0; i < persons.length; i++) {
- console.log(persons[i].name + " " + persons[i].surname)
- }
- //loop of loop of values
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- }
- ]
- for (i = 0; i < persons.length; i++) {
- for (let key in persons[i]) {
- console.log(persons[i][key])
- }
- }
- //fullName
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- }
- ]
- for (let newkey of persons) {
- if (newkey.name && newkey.surname) {
- newkey.fullName = newkey.name + ' ' + newkey.surname;
- }
- } console.log(persons)
- //serialize
- JSON.stringify(persons)
- '[{"name":"Andrey","surname":"Ivanov","fathername":"Andreevich","age":21,"sex":"male"},{"name":"Igor","surname":"Vasin","fathername":"Igorevich","age":31},{"name":"Oleg","surname":"Petrov","fathername":"Olegovich"}]'
- //deserialize
- '{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}'
- let d = '{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}'
- d = JSON.parse('{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}')
- persons[3] = d;
- console.log(persons)
- // HTML
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- },
- d = {
- name: "Anna",
- surname: "Petrova",
- fathername: "Igorevna",
- age: 24
- }
- ]
- let str = "<table border='1' cols = 4>"
- for (let i = 0; i < persons.length; i++) {
- str += `<tr><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
- }
- str += "</table>"
- console.log(str)
- document.write(str)
- //HTML optional fields
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- },
- d = {
- name: "Anna",
- surname: "Petrova",
- fathername: "Igorevna",
- age: 24
- }
- ]
- let str = "<table border='1' cols = 4>"
- for (let i = 0; i < 4; i++) {
- for (let key in persons[i]) {
- str += `<tr><td>${key}</td><td>${persons[i][key]}</td></tr>`
-
- }
- }str += "</table>"
- console.log(str)
- document.write(str)
- //HTML tr color
- let persons = [
- a = {
- name: "Andrey",
- surname: "Ivanov",
- fathername: "Andreevich",
- age: 21,
- sex: "male"
- },
- b = {
- name: "Igor",
- surname: "Vasin",
- fathername: "Igorevich",
- age: 31
- },
- c = {
- name: "Oleg",
- surname: "Petrov",
- fathername: "Olegovich"
- },
- d = {
- name: "Anna",
- surname: "Petrova",
- fathername: "Igorevna",
- age: 24
- }
- ]
- let str = "<table border='1' cols = 4>"
- for (let i = 0; i < 4; i++) {
- for (let key in persons[i]) {
- str += `<tr style = "color: green; ><td>${key}</td><td>${persons[i][key]}</td></tr>`
-
- }
- }str += "</table>"
- console.log(str)
- document.write(str)
- //destruct array
- if (key in a, b, c)
- //destruct array
- let arr = [1, 2, 3, 4, 5, "a", "b", "c"]
- let [odd1, even1, odd2, even2, odd3, ...abc] = arr;
- //destruct string
- let arr = [1, "abc"]
- let [number, ...abc] = arr;
- let [a, b, c] = "abc";
- let [s1, s2, s3] = [a, b, c];
- //destruct 2
- let obj = {
- name: 'Ivan',
- surname: 'Petrov',
- children: [{ name: 'Maria' }, { name: 'Nikolay' }]
- }
- let {
- children:
- [{ name: name1 },
- { name: name2 }
- ] } = obj;
- //destruct 3
- let number = [1, 2, 3, 4, 5, 6, 7, 10];
- let [a, b, { length = number.length }] = number
- </script>
- </body>
- </html>
|