//Literals
{
var ford = {
type: "hybrid",
model: "fusion",
power: 180,
size: "4800/150/130",
fuel: "petrol",
};
}
//Literals expand
var ford = {
type: "hybrid",
[prompt('Введите название ключа')]: [prompt('Введите параметр')].join(' '),
power: 180,
size: "4800/150/130",
fuel: "petrol",
};
//Literals copy
let property = prompt('Введите свойство')
const obj = {...ford}
obj.type = property
console.log(obj)
//Html tree
let body = {
tagName: 'body',
children: [{
tagName: 'div',
children: [{
tagName: 'span',
children: ["Enter a data please:"]
},
{
tagName: 'br',
},
{
tagName: 'input',
attrs: {
children: {
type:'text',
id:'name',
}
},
},
{
tagName: 'input',
attrs: {
children: {
type:'text',
id:'surname',
}
},
}]
},
{
tagName: 'div',
children: [{
tagName: 'button',
attrs: {
[attributes]: "ok",
},
children:['OK']
},
{
tagName: 'button',
attrs: {},
}]
}]
}
body.children[1].children[1].children=["Cancel"];
body.children[1].children[1].attrs.id= "cancel";
//Parent
body.children[0].parent = body;
body.children[1].parent = body;
body.children[0].children[0].parent = body.children[0];
body.children[0].children[1].parent = body.children[0];
body.children[0].children[2].parent = body.children[0];
body.children[0].children[3].parent = body.children[0];
body.children[1].children[0].parent = body.children[1];
body.children[1].children[1].parent = body.children[1];
//destruct array
{
let arr = [1,2,3,4,5, "a", "b", "c"]
let [odd1,even1,odd2,even2,odd3,...arr2]=arr
console.log(odd1,even1,odd2,even2,odd3,arr2)//1 2 3 4 5 [ 'a', 'b', 'c' ]
}
//destruct string
{
let arr = [1, "abc"]
let [number,arr2]=arr
let [s1,s2,s3]=arr2
console.log(number,s1,s2,s3)// 1 a b c
}
//destruct 2
{
let obj = {
name: 'Ivan',
surname: 'Petrov',
children: [{name: 'Maria'}, {name: 'Nikolay'}]
}
let {children:[{name:name1},{name:name2}]}= obj
console.log(name1,name2)//Maria Nikolay
}
//destruct 3
{
let arr = [1,2,3,4, 5,6,7,10]
let {0:a,1:b,length}= arr
console.log(a,b,length)//1 2 8
}
//Destructure и Change OK
{
let attributes = prompt('Введите название атрибута')// это к заданию Change OK
let body = {
tagName: 'body',
children: [{
tagName: 'div',
children: [{
tagName: 'span',
children: ["Enter a data please:"]
},
{
tagName: 'br',
},
{
tagName: 'input',
attrs: {
children: {
type:'text',
id:'name',
}
},
},
{
tagName: 'input',
attrs: {
type:'text',
id:'surname',
},
}]
},
{
tagName: 'div',
children: [{
tagName: 'button',
attrs: {
[attributes]: "ok",
},
children:['OK']
},
{
tagName: 'button',
children:["Cancel"],
attrs: {
id: "cancel"
},
}]
}]
}
let {children:[{children:[{children:[textSpan]},{},{},{attrs:{id:textSurname}}]},{children:[{},{children:[textCancel]}]}]}=body//это к заданию Destructure
console.log(textSpan,textCancel,textSurname)//Enter a data please: Cancel surname //это к заданию Destructure
body.children[1].children[0].attrs.attributes= prompt('Введите значение атрибута');// это к заданию Change OK
}
//Copy delete
{
let ford = {
type: "hybrid",
model: "fusion",
power: 180,
size: "4800/150/130",
fuel: "petrol",
};
let key = prompt('Введите ключ для удаления')
const {[key]:x, ...obj} = ford
console.log(obj)
}
//Currency real rate
{
fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
.then(data => {
let currency = prompt('Введите исходную валюту').toUpperCase()
let currency2 = prompt('валюту, в которую происходит конвертация').toUpperCase()
let sum = +prompt('Введите сумму в '+currency2)
let rates1 = data.rates[currency]
let rates2 = data.rates[currency2]
data = (rates1/rates2)*sum
if (currency && currency2 && sum && data){
alert('Результат '+data+' '+currency)}
else{alert('Ошибка')}
})
}
//Currency drop down
{
fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
.then(data => {
let key = data.rates
key = Object.keys(key)
key = key.map(x=> "")
key = ''
console.log(document.write(key))
})
}
//Currency table
{
fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
.then(data => {
let dataObj = data.rates
let arrValues = Object.values(dataObj)
let arrKeys = Object.keys(dataObj)
let arrData = []
let i = 0
for(let valuesEl of arrValues){
arrData.push(arrValues.map(x=>(valuesEl/x).toFixed(3)))
arrData[i].unshift(arrKeys[i])
i++
}
arrKeys.unshift('')
arrData.unshift(arrKeys)
let currencyTable = "
"
for (let arrEl of arrData){
currencyTable += ''
for (let cell of arrEl){
currencyTable += ''+cell+' | '
}
currencyTable+= '
'
}
currencyTable+= "
"
document.write(currencyTable)
})
}
//Form
{
const car = {
"Name":"chevrolet chevelle malibu",
"Cylinders":8,
"Displacement":307,
"Horsepower":130,
"Weight_in_lbs":3504,
"Origin":"USA",
"in_production": false
}
let keysArrCar = Object.keys(car)
let valuesArrCar = Object.values(car)
let i = 0
let strType
let formObjCar = ''
document.write(formObjCar)
}
//Table
// На тестовых данных проверено
{
const persons = [
{
name: 'Мария',
fatherName: 'Ивановна',
surname: 'Иванова',
sex: 'female'
},
{
name: 'Николай',
fatherName: 'Иванович',
surname: 'Иванов',
age: 15
},
{
name: 'Петр',
fatherName: 'Иванович',
surname: 'Иванов',
married: true
},
]
let i = 0
let arrKeys = []
for (let x of persons){
arrKeys += Object.keys(persons[i])+','
i++
}
arrKeys = arrKeys.split(',').slice(0,-1)
const makeUniq = (arr) => {
return arr.filter((el1, el2) => arr.indexOf(el1) === el2);
}
arrKeys = makeUniq(arrKeys)
let arrValue = []
for (let objPersons of persons){
arrValue.push(arrKeys.map(el=>objPersons[el]===undefined?objPersons[el]=' ':objPersons[el]))
}
arrValue.unshift(arrKeys)
let arrTable = ""
for (let arrEl of arrValue){
arrTable += ''
for (let cell of arrEl){
arrTable += ''+cell+' | '
}
arrTable+= '
'
}
arrTable+= "
"
document.write(arrTable)
}