const buttonSelector = '.button';
const blockSelector = '.div';
const activeClass = 'red';
function toggleBlock(blocks, buttons, button) {
const index = Array.prototype.indexOf.call(buttons, button);
blocks.forEach((n, i) => {
n.classList[i === index ? 'toggle' : 'remove'](activeClass);
});
}
// обработчик клика делегированный, назначается один раз
document.addEventListener('click', e => {
const button = e.target.closest(buttonSelector);
if (button) {
const blocks = document.querySelectorAll(blockSelector);
const buttons = document.querySelectorAll(buttonSelector);
toggleBlock(blocks, buttons, button);
}
});
// или, назначаем обработчик клика каждой кнопке индивидуально
const buttons = document.querySelectorAll(buttonSelector);
const blocks = document.querySelectorAll(blockSelector);
const onClick = e => toggleBlock(blocks, buttons, e.currentTarget);
buttons.forEach(n => n.addEventListener('click', onClick));
(1..height_result - 1).each do |n|
space_count = (height.to_i - n).abs
asterisk_count = height_result - space_count * 2 - 1
print " " * space_count + "*" * asterisk_count + "\n"
end
На основе этого значения надо менять результат внутри callback-функции передаваемой вторым параметром usort()
function array_sort(array &$arr, $key, $sort = 'asc') {
$sort = $sort === 'asc' ? 1 : -1;
usort($arr, function($a, $b) use($sort, $key) {
$a = $a[$key];
$b = $b[$key];
return $sort * ($a === $b ? 0 : $a > $b ? 1 : -1);
});
}
<div class="spinner"><div>
.spinner {
color: black;
font-size: 5rem;
}
.spinner::before {
display: inline-block;
text-align: center;
font-family: monospace;
width: 5rem;
content: "";
animation: spinner .8s infinite steps(4);
}
@keyframes spinner {
0%, 100% { content: "\2014"; }
25% { content: "\\"; }
50% { content: "|"; }
75% { content: "/"; }
}
<div class="spinner"><div>
.spinner {
color: black;
font-size: 5rem;
font-family: monospace;
}
const block = document.querySelector('.spinner');
const text = [ '\u2014', '\\', '|', '/' ];
let index = -1;
setInterval(() => {
index = (index + 1) % text.length;
block.textContent = text[index];
}, 200);
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<g font-size="60" font-family="monospace" fill="black" visibility="hidden">
<text x="30" y="60">
—
<animate attributeName="visibility" from="hidden" to="visible" begin="0.2s; hide4.begin" dur="0.2s" id="show1" />
<animate attributeName="visibility" from="visible" to="hidden" begin="show1.begin + 0.2s" dur="0.2s" id="hide1" />
</text>
<text x="30" y="60">
\
<animate attributeName="visibility" from="hidden" to="visible" begin="hide1.begin" dur="0.2s" id="show2" />
<animate attributeName="visibility" from="visible" to="hidden" begin="hide1.begin + 0.2s" dur="0.2s" id="hide2" />
</text>
<text x="30" y="60">
|
<animate attributeName="visibility" from="hidden" to="visible" begin="hide2.begin" dur="0.2s" id="show3" />
<animate attributeName="visibility" from="visible" to="hidden" begin="hide2.begin + 0.2s" dur="0.2s" id="hide3" />
</text>
<text x="30" y="60">
/
<animate attributeName="visibility" from="hidden" to="visible" begin="hide3.begin" dur="0.2s" id="show4" />
<animate attributeName="visibility" from="visible" to="hidden" begin="hide3.begin + 0.2s" dur="0.2s" id="hide4" />
</text>
</g>
</svg>
Vue.component('v-tooltip', {
template: `
<div class="wrp">
<div class="btn" @click="visible = !visible">{{ label }}</div>
<div class="tooltip" :class="{ visible }">{{ message }}</div>
</div>`,
props: {
label: {
type: String,
default: 'click me',
},
message: {
type: String,
default: 'hello, world!!',
},
},
data: () => ({
visible: false,
}),
});
@input="onInput($event, product)"
methods: {
onInput(e, product) {
product.quantity = Math.max(0, parseInt(e.target.value) || 0);
},
},
watch: {
products: {
deep: true,
handler() {
this.products.forEach(n => n.quantity = Math.max(0, parseInt(n.quantity) || 0));
},
},
},
<input class="date">
<input class="date">
<input class="date">
const yearButton = year => `
<button
data-year="${year}"
class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all"
>${year}</button>
`;
$('.date').datepicker({
showButtonPanel: true,
}).each(function(i) {
$(this).datepicker(
'option',
'currentText',
`Today ${yearButton(2015 + i * 2)}${yearButton(2015 + i * 2 + 1)}`
);
});
$(document).on('click', '[data-year]', function() {
$.datepicker._curInst.input.datepicker('setDate', `01/01/${$(this).data('year')}`);
});
$result = array_map(function($n) {
return implode(', ', array_column($n, 'value'));
}, $arr);
const nestedToPlain = (obj, path = '') =>
Object.entries(obj).reduce((acc, [ k, v ]) => {
const newPath = `${path}${path ? '.' : ''}${k}`;
return Object.assign(acc, v instanceof Object
? nestedToPlain(v, newPath)
: { [newPath]: v }
);
}, {});
const plain = nestedToPlain(example, 'example');
const nestedToPlain = (obj, keys = []) =>
Object.entries(obj).reduce((acc, [ k, v ]) => (
keys.push(k),
Object.assign(acc, v instanceof Object
? nestedToPlain(v, keys)
: { [keys.join('.')]: v }
),
keys.pop(),
acc
), {});
const plain = nestedToPlain(example, [ 'example' ]);
const nestedToPlain = function(obj, keys = []) {
const result = {};
const [ push, pop ] = this;
for (const stack = [ obj ]; stack.length;) {
const n = stack.pop();
if (n instanceof Object) {
Object.entries(n).reverse().forEach(([ k, v ]) => stack.push(pop, v, k, push));
} else if (n === push) {
keys.push(stack.pop());
} else if (n === pop) {
keys.pop();
} else {
result[keys.join('.')] = n;
}
}
return result;
}.bind([ Symbol(), Symbol() ]);
const plain = nestedToPlain(example, [ 'example' ]);
const newArr = arr
.filter(function(n) {
return !(this[n.country] = this.hasOwnProperty(n.country));
}, {})
.map((n, i) => ({ id: i + 1, country: n.country }));
const newArr = Object.values(arr.reduce((acc, { country }) => {
acc[0][country] = acc[0][country] || { id: ++acc[1], country };
return acc;
}, [ {}, 0 ])[0]);
const newArr = Array.from(arr.reduce((acc, { country: n }) => (
acc.set(n, acc.get(n) || { id: -~acc.size, country: n })
), new Map).values());
state = {
active: null,
}
toggle = ({ target: { dataset: { name } } }) => {
this.setState(({ active }) => ({
active: active === name ? null : name,
}));
}
<button onClick={this.toggle} data-name="collapse_1"></button>
<button onClick={this.toggle} data-name="collapse_2"></button>
<button onClick={this.toggle} data-name="collapse_3"></button>
toggle = ({ target: { dataset: { name } } }) => {
this.setState(({ collapse }) => ({
collaple: {
...collapse,
[name]: !collapse[name],
},
}));
}
const keys = [ 'firstName', 'lastName' ];
const values = document.querySelector('input').value.toLowerCase().match(/\S+/g) || [];
const result = arr.filter(n => keys.some(k => values.some(v => n[k].toLowerCase().includes(v))));