|
@@ -0,0 +1,144 @@
|
|
|
+/* 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[4] = 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 = arr2[0]
|
|
|
+ let s1 = arr2[1][0]
|
|
|
+ let s2 = arr2[1][1]
|
|
|
+ let s3 = arr2[1][2]
|
|
|
+
|
|
|
+/* destruct 2 */
|
|
|
+
|
|
|
+ let obj = {name: 'Ivan',
|
|
|
+ surname: 'Petrov',
|
|
|
+ children: [{name: 'Maria'}, {name: 'Nikolay'}]}
|
|
|
+
|
|
|
+ let name1 = obj.children[0]
|
|
|
+ let name2 = obj.children[1]
|
|
|
+
|
|
|
+/* destruct 3 */
|
|
|
+
|
|
|
+ let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];
|
|
|
+ let [a, b, { length = arr3.length }] = arr3
|