const groupedData = data.reduce((acc, {sport, sub}) => {
const elem = acc.find((item) => item.sport === sport);
if (elem) {
elem.subs.push(sub);
} else {
acc.push({
sport,
subs: sub ? [sub] : []
});
}
return acc;
}, []);
const $ul = $('<ul>', {
id: 'list'
}).appendTo('body');
const elems = groupedData.map(({sport, subs}) => {
return `
<li>${sport}<ul>
${subs.map((sub) => `<li>${sub}</li>`).join('')}
</ul>
</li>
`;
}).join('');
$ul.append(elems);
function ajax() {
return fetch(target, fetchOptions)
.then((response) => {
if (!target) throw new Error('Invalid url');
if (response.ok) return response.json();
throw new Error(`${response.status} ${response.statusText}`);
})
.then((json) => {
if (json['statusOk']) return json;
throw new Error(json['message'] || 'Server response was not ok');
});
}
ajax(action, { body: formData })
.then((json) => {
console.log('RESPONSE', json);
})
.catch((error) => {
console.error(error);
});
function myFunc(cb){
var err = true;
// имитируем асинхронную операцию
setTimeout(function(){
cb(err);
}, 10);
}
try {
myFunc(function(err){
if (err) throw new Error('Oops');
});
alert('Всё как бы хорошо!');
} catch(e) {
alert(e.message);
}
function myFunc(){
let promise = new Promise((resolve, reject) => {
let err = true;
// имитируем асинхронную операцию
setTimeout(() => {
if (err) {
reject('Ooops!');
} else {
resolve(123);
}
}, 10);
});
return promise;
}
myFunc()
.then(data => {
alert('Всё точно хорошо!');
})
.catch(e => {
alert(e);
});
function transfer(from, to) {
$(from).on('click', 'button', function(e) {
const $el = $(e.target).parent();
const index = +$el.data('index');
const indices = $('li', to).get().map(n => +n.dataset.index);
if (index < indices[0] || !indices.length) {
$(to).prepend($el);
} else if (index > indices[indices.length - 1]) {
$(to).append($el);
} else {
$(`${to} [data-index="${indices.filter(n => n < index).pop()}"]`).after($el);
}
});
}
transfer('#parent1', '#parent2');
transfer('#parent2', '#parent1');
let json = [
{"location": {
"country": {
"name": "Finland"
},
"city":"Helsinki"
}},
{"location": {
"country": {
"name": "Russia",
},
"city":"Moscow"
}},
{"location": {
"country": {
"name": "Russia",
},
"city":"Novgorod"
}},
{"location": {
"country": {
"name": "USA",
},
"city":"Boston"
}},
{"location": {
"country": {
"name": "Russia",
},
"city":"Ufa"
}},
{"location": {
"country": {
"name": "Australia",
},
"city":"Sidney"
}}
];
let wow = {
db: {},
parse(data) {
data.forEach(function({location: {country: {name: country}, city}} = node){
!this.db[country] ? (this.db[country] = {}, this.db[country][city] = true) : this.db[country][city] = true;
}, this);
return this;
},
render() {
function renderList(data) {
return `<ul>${renderCountries(data)}</ul>`
}
function renderCountries(data, html = '') {
Object.keys(data).forEach(country => html += `<li>${country}<ul>${renderCities(data[country])}</ul></li>`);
return html;
}
function renderCities(data, html = '') {
Object.keys(data).forEach(city => html += `<li>${city}</li>`);
return html;
}
return `<ul>${renderList(this.db)}</ul>`;
}
};
document.querySelector('body').innerHTML = wow.parse(json).render();
let arr =
[
{
"location":{
"country":{
"name":"Finland",
"id":"FI"
},
"city":"Helsinki"
}
},
{
"location":{
"country":{
"name":"England",
"id":"FI"
},
"city":"London"
}
},
{
"location":{
"country":{
"name":"Russian",
"id":"RU"
},
"city":"Moskow"
}
},
{
"location":{
"country":{
"name":"Russian",
"id":"RU"
},
"city":"Saint Petersburg"
}
},
{
"location":{
"country":{
"name":"Russian",
"id":"RU"
},
"city":"Sevastopol"
}
},
];
let all = [];
let bool = true;
for (var i = 0; i < arr.length; i++) {
for (var n = 0; n <= all.length; n++) {
if(all.length == 0) all.push({"country":arr[i].location.country.name,"city":[arr[i].location.city]});
bool = true;
for (var d = 0; d < all.length; d++) {
if(all[d].country==arr[i].location.country.name){
bool = false;
if(all[d].city.indexOf(arr[i].location.city)<0)all[d].city.push(arr[i].location.city);
};
}
if(bool)all.push({"country":arr[i].location.country.name,"city":[arr[i].location.city]});
}
}
console.log(all)
[
{
country: 'Finland',
city: ['Helsinki']
},
{
country: 'England',
city: ['London']
},
{
country: 'Russian',
city: ['Moskow', 'Saint Petersburg', 'Sevastopol']
}
]
<?php
if(!is_admin()) { wp_enqueue_style( 'style-theme', get_template_directory_uri() . '/style.min.css', array(), '1.0.0', 'all');
}
?>
чтобы реализация проекта прошла без косяков, задержек сроков, недоразумений и прочего? Какие моменты учитываете при планировании, как ставите задачи (технические и организационные)?
li(class= classname + 'item')!= item
screen.availHeight
let element = document.querySelector("Ваш элемент");
document.onscroll = () => {
let percent = ( screen.availHeight - window.pageYOffset / 2 ) / screen.availHeight;
if(percent <= 0) element.style.opacity = 0;
else element.style.opacity = percent;
console.log(percent);
};