function group(data, key, val = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const getVal = val instanceof Function ? val : n => n[val];
const result = {};
for (const n of data) {
const k = getKey(n);
(result[k] = result[k] || []).push(getVal(n));
}
return result;
}group(arr, 'room')
// или
group(arr, n => n.room)group(Array(5).keys(), n => n % 2 ? 'нечётные' : 'чётные')group('a8-C!39', n => (
n.toLowerCase() !== n.toUpperCase() ? 'буква' :
!Number.isNaN(+n) ? 'цифра' :
'другое'
))<input name="xxx" value="69">
<input name="xxx" value="187">
<input name="xxx" value="666">
<input name="yyy" value="-1">group(document.querySelectorAll('input'), 'name', n => +n.value)
const containers = document.querySelectorAll('.counts');
const tag = 'div';
const className = 'count';
const count = 3;const html = [...Array(count)]
.map((_, i) => `<${tag} class="${className}">${i + 1}</${tag}>`)
.join('');
for (const n of containers) {
n.insertAdjacentHTML('beforeend', html);
}for (let i = 0; i < containers.length; i++) {
for (let j = 0; j < count;) {
const el = document.createElement(tag);
el.classList.add(className);
el.innerText = ++j;
containers[i].appendChild(el);
}
}const fragment = document.createDocumentFragment();
fragment.append(...Array.from({ length: count }, (_, i) => (
Object.assign(document.createElement(tag), {
textContent: -~i,
className,
})
)));
containers.forEach(n => n.append(fragment.cloneNode(true)));
state: {
page: 0,
perPage: 5,
total: 0,
posts: [],
loading: false,
},getters: {
numPages: state => Math.ceil(state.total / state.perPage),
},mutations: {
updateLoading: (state, loading) => state.loading = loading,
updatePosts: (state, { posts, total, page }) => Object.assign(state, { posts, total, page }),
},actions: {
async fetchPosts({ state, commit }, page) {
commit('updateLoading', true);
const start = (page - 1) * state.perPage;
const end = page * state.perPage;
const url = `https://jsonplaceholder.typicode.com/posts?_start=${start}&_end=${end}`;
try {
const response = await fetch(url);
const posts = await response.json();
const total = response.headers.get('x-total-count');
commit('updatePosts', { posts, total, page });
} catch (e) {
console.error(e);
}
commit('updateLoading', false);
},
},props: [ 'page', 'numPages' ],methods: {
goTo(page) {
this.$emit('paginate', page);
},
next(step) {
this.goTo(this.page + step);
},
},computed: {
isFirst() {
return this.page <= 1;
},
isLast() {
return this.page >= this.numPages;
},
},<button @click="goTo(1)" :disabled="isFirst"><<</button>
<button @click="next(-1)" :disabled="isFirst"><</button>
{{ page }} / {{ numPages }}
<button @click="next(+1)" :disabled="isLast">></button>
<button @click="goTo(numPages)" :disabled="isLast">>></button>v-model:model: {
prop: 'page',
event: 'paginate',
},computed: {
page: {
get() {
return this.$store.state.page;
},
set(page) {
this.$store.dispatch('fetchPosts', page);
},
},<компонент-пагинации
v-model="page"
:num-pages="$store.getters.numPages"
/>created() {
this.page = 1;
},<div v-if="$store.state.loading">данные загружаются, ждите</div>
<компонент-для-отображения-данных v-else :данные="$store.state.posts" />
const siblings = Array.prototype.filter.call(
this.parentNode.children,
n => n !== this
);const siblings = [];
for (let el = this; (el = el.previousElementSibling); siblings.unshift(el)) ;
for (let el = this; (el = el.nextElementSibling); siblings.push(el)) ;
$('.openFormPopup').click(e => $('#btn-clicked').val(e.target.dataset.title));
const asyncForEach = (data, callback) =>
Array.prototype.reduce.call(
data,
(promise, n, i, a) => promise.then(() => callback(n, i, a)),
Promise.resolve()
).then(() => {});
// или
async function asyncForEach(data, callback) {
for (const [ i, n ] of data.entries()) {
await callback(n, i, data);
}
}asyncForEach(
document.querySelectorAll('li'),
li => new Promise(r => setTimeout(() => r(console.log(li.textContent)), 1000))
);
Здесь только регуляркой или как?
function incrementNumberInString(str) {
for (let i = 0, iNum = -1; i < str.length; i++) {
if ('0123456789'.includes(str[i])) {
if (iNum === -1 && str[i - 1] === '[') {
iNum = i;
}
} else if (iNum !== -1 && str[i] === ']') {
return str.slice(0, iNum) + (Number(str.slice(iNum, i)) + 1) + str.slice(i);
} else {
iNum = -1;
}
}
return str;
}str.replace(/(?<=\[)\d+(?=\])/, m => ++m)
// или
str.replace(/\[\d+\]/, m => '[' + (m.slice(1, -1) - -1) + ']')
// или
str.replace(/\[(\d+)\]/, (m, g1) => `[${-~g1}]`)
как лучше реализовать функционал, чтобы иконка была одна, а в нужных компонентах мы докидывали цвет?
выводит побуквенно
v-for так и перебирает - "побуквенно". То есть, значениями user являются строки единичной длины. Ну а свойств id, name, price и т.д. у строк нет, отсюда пустота там, где вы выводите свойства элементов users.
почему не удаляется item, а добавляется?
slice(0, -1)) плюс полная копия (slice(0)).deleteItem = id => {
this.setState(({ todoData }) => ({
todoData: todoData.filter(n => n.id !== id),
}));
}