const ul = document.querySelector('.ul-parent');
const groupedNodes = Array.prototype.reduce.call(
ul.children,
(acc, n) => {
const k = n.textContent[0].toLowerCase();
(acc[k] = acc[k] ?? []).push(n);
return acc;
},
{}
);
ul.replaceWith(...Object.entries(groupedNodes).map(n => {
const ul = document.createElement('ul');
ul.classList.add('ul-parent', n[0] + '-litter');
ul.append(...n[1]);
return ul;
}));
const ul = document.querySelector('.ul-parent');
const groupedHTML = Array
.from(ul.querySelectorAll(':scope > li'))
.reduce((acc, { innerText: [ c ], outerHTML }) => {
const k = c.toLowerCase();
(acc[k] = acc[k] ?? []).push(outerHTML);
return acc;
}, {});
ul.outerHTML = Object
.entries(groupedHTML)
.map(([ k, v ]) => `<ul class="ul-parent ${k}-litter">${v.join('')}</ul>`)
.join('');
document.addEventListener('click', e => {
const div = e.target.closest('button')?.closest('div');
if (div) {
console.log(div.id);
}
});
document.querySelectorAll('div button').forEach(function(n) {
n.addEventListener('click', this);
}, e => console.log(e.currentTarget.closest('div').id));
arr.sort((a, b) => (a.rooms - b.rooms) || (a.square - b.square));
// или, в более общем виде
const sort = (arr, ...keys) =>
arr.sort((a, b) => {
let diff = 0;
keys.find(k => diff = a[k] - b[k]);
return diff;
});
sort(arr, 'rooms', 'square');
const sorted = (arr, keys) => arr
.map(n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
for (let i = 0; ++i < a.length && !(diff = a[i] - b[i]);) ;
return diff;
})
.map(n => n[0]);
const sortedArr = sorted(arr, n => [ n.rooms, n.square ]);
for i, (k, v) in enumerate(sorted(top_list.items(), key=lambda n: n[1], reverse=True), 1):
print(f'{i}) {k} - {v}')
data: () => ({
items: [],
}),
v-for
экземпляры дочернего компонента, к которым цепляете посредством v-model
элементы массива:<child-component v-for="(n, i) in items" v-model="items[i]"></child-component>
props: [ 'value' ],
computed: {
innerValue() {
return new Proxy(this.value, {
set: (target, prop, val) => {
this.$emit('input', { ...target, [prop]: val });
return true;
},
});
},
},
v-model
:<input type="text" v-model="innerValue.someStringProp">
<input type="checkbox" v-model="innerValue.someBooleanProp">
<input type="number" v-model.number="innerValue.someNumberProp">
onChange = ({ target: { value, dataset: { objName, key } } }) => {
this.setState(({ [objName]: obj }) => ({
[objName]: {
...obj,
[key]: value,
},
}));
}
<input data-obj-name="fr" data-key="name" value={this.state.fr.name} onChange={this.onChange} />
<input data-obj-name="en" data-key="title" value={this.state.en.title} onChange={this.onChange} />
data: () => ({
repeat: 0,
}),
<child-component v-for="i in repeat"></child-component>
<button @click="repeat++">add one more child component instance</button>
const $table = $('table');
const columnIndex = // здесь должен быть номер столбца с числами
const sum = $table
.find('tr:visible')
.get()
.reduce((acc, n) => acc + +$('td', n).eq(columnIndex).text(), 0);
$table
.find('th')
.eq(columnIndex)
.text(`Вес (${sum})`);
class App extends React.Component {
state = {
cells: Array(16).fill(null),
probability: 0.7,
}
onClick = e => {
const index = +e.target.dataset.index;
this.setState(({ cells, probability }) => ({
cells: cells.map((n, i) => i === index && !n
? (Math.random() < probability ? 'black' : 'red')
: n
),
}));
}
render() {
return (
<div className="ColorS">
{this.state.cells.map((n, i) => (
<div
className="ColorS-grid"
style={n && { background: n }}
data-index={i}
onClick={this.onClick}
/>
))}
</div>
);
}
}
$('.news-block-2').each((i, n) => $('.text-news', n).after($('.picture-news', n)));
$('.news-block-2 tr').append(function() {
return $('.picture-news', this);
});
$('.news-block-2 .text-news').each((i, n) => $(n).parent().prepend(n));
$('.news-block-2 .picture-news').before(function() {
return $(this).next();
});
X = n => n ? (n & 1 ? '-chirp' : '') + X(n >> 1) + X(n >> 1) : ''
chirp = n => X(n).slice(1)
google.visualization.arrayToDataTable(data, true)