123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /* 3 persons */
- let a = {
- name: 'Andriy',
- surname: 'Yarmolenko'
- }
- let b = {
- name: 'Oleksandr',
- surname: 'Zinchenko'
- }
- let c = {
- name: 'Vitaliy',
- surname: 'Mykolenko'
- }
- /* different fields */
- a.age = '32',
- a.nationality = 'Ukrainian',
- a.club = 'West Ham United'
- b.age = '25',
- b.nationality = 'Ukrainian',
- b.club = 'Manchester City'
- c.age = '23',
- c.nationality = 'Ukrainian',
- c.club = 'Everton'
- /* fields check */
- if ('nationality' in a, b, c) {
- alert(a.surname + ' ' + a.name + ' ' + a.nationality + '\n' + b.surname + ' ' + b.name + ' ' + b.nationality + '\n' + c.surname + ' ' + c.name + ' ' + c.nationality)
- }
- /* array of persons */
- let persons = []
- persons.push(a, b, c)
- persons.push({name: 'Ruslan', surname: 'Malynovskiy', age: '29', nationality: 'Ukrainian', club: 'Atalanta'})
- /* loop of persons */
- for (let i = 0; i < persons.length; i++) {
- console.log(persons[i])
- }
- /* loop of name and surname */
- for (let i = 0; i < persons.length; i++) {
- console.log(persons[i].name + ' ' + persons[i].surname)
- }
- /* loop of loop of values */
- for (let i = 0; i < persons.length; i++) {
- for (let key in persons[i]) {
- console.log(persons[i][key])
- }
- }
- /* fullName */
- for (let newObject of persons) {
- if (newObject.name && newObject.surname) {
- newObject.fullName = newObject.name + ' ' + newObject.surname
- }
- }
- /* serialize */
- JSON.stringify(persons)
- /* deserialize */
- let d = JSON.parse('{"name" : "Andriy", "surname" : "Lunin", "age" : "23", "nationality" : "Ukrainian", "club" : "Real Madrid"}')
- persons.push(d)
- /* HTML */
- var str = "<table border='1'>"
- 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 */
- var str = "<table border='1'>"
- for (let i = 0; i < persons.length; 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 */
- var str = "<table border='1'>"
- for (let i = 0; i < persons.length; i++) {
- for (let key in persons[i]) {
- str += `<tr style = "color : blue;"><td>${key}</td><td>${persons[i][key]}</td></tr>`
-
- }
- }str += "</table>"
- console.log(str)
- document.write(str)
- /* destruct array */
- let arr = [1,2,3,4,5, "a", "b", "c"]
- let [odd1, even1, odd2, even2, odd3, ...abc] = arr
- /* destruct string */
- let arr2 = [1, "abc"]
- let [number, [s1, s2, s3]] = arr2
- /* destruct 2 */
- let obj = {name: 'Ivan',
- surname: 'Petrov',
- children: [{name: 'Maria'}, {name: 'Nikolay'}]}
-
- let {children: [{name: name1}, {name: name2}]} = obj
- /* destruct 3 */
- let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];
- let {0:a, 1:b, length} = arr3
|