const urls = [
'http://sitename.net/somedirectory/14545/',
'http://sitename.net/somedirectory/14545/count-1145',
'http://sitename.net/somedirectory/14545/specific-345',
'http://sitename.net/somedirectory/14545/count-1145/specific-345',
];
const getParams = (url, params) =>
params instanceof Array && params.length
? (url.match(RegExp(`(${params.join('|')})-\\d+`, 'g')) || [])
.map(n => n.split('-'))
.reduce((acc, n) => (acc[n[0]] = n[1], acc), {})
: {};
const params = urls.map(n => getParams(n, [ 'count', 'specific' ]));
const getParams = url =>
Object.fromEntries((url
.match(/[a-z]+-\d+/g) || [])
.map(n => n.split('-'))
);
.dropdown-checkbox ul.opened {
display: block;
}
<label @click="opened = !opened">click me</label>
<ul :class="{ opened }">
...
data: () => ({
opened: false,
}),
const [ first, second ] = [...arr]
.sort((a, b) => b.id - a.id)
.slice(0, 2)
.map(n => n.name);
const [ { name: first }, { name: second } ] = arr.reduce((acc, n) => {
if (!(acc[0].id >= n.id)) {
acc[1] = acc[0];
acc[0] = n;
} else if (!(acc[1].id >= n.id)) {
acc[1] = n;
}
return acc;
}, [ {}, {} ]);
cildren
const copyTree = (tree, f) =>
tree.map(n => {
n = f instanceof Function ? f(n) : { ...n };
if (n.children instanceof Array) {
n.children = copyTree(n.children, f);
}
return n;
});
const dataCopy = copyTree(data, ({ name: key, ...x }) => ({ key, ...x }));
[...Array(n)]
. Но, учитывая реально решаемую задачу, не надо никаких массивов, состоящих из undefined, заполняйте сразу тем, чем надо:const sequence = (length, pattern) =>
pattern instanceof Function
? Array.from({ length }, pattern)
: Array(length).fill(pattern);
const select = document.querySelector('#country');
const value = 'europe';
const options = select.querySelectorAll(`option[value*="${value}"]`);
const options = Array.prototype.filter.call(
select.options,
n => n.value.includes(value)
);
const options = [];
for (const n of select.children) {
if (n.value.indexOf(value) !== -1) {
options.push(n);
}
}
document.querySelector('.allstatus-btn').addEventListener('click', () => {
const items = Object.fromEntries(Array.from(
document.querySelectorAll('.number-all'),
n => [ n.innerText, n ]
));
fetch('https://api.novaposhta.ua/v2.0/json/', {
method: 'POST',
body: JSON.stringify({
modelName: 'TrackingDocument',
calledMethod: 'getStatusDocuments',
methodProperties: {
Documents: Object.keys(items).map(n => ({
DocumentNumber: n,
Phone: '',
})),
},
apiKey: '',
}),
})
.then(r => r.json())
.then(r => r.data.forEach(n => items[n.Number].innerHTML += n.Status));
});
this.setState(({ events }) => ({
events: events.map(n => n.id === id ? { ...n, title: newTitle } : n),
}));
function createTree(data) {
const obl = {};
const org = {};
const podr = {};
data.forEach(n => {
obl[n.obl.id] = { ...n.obl, children: [] };
org[n.org.id] = { ...n.org, children: [] };
podr[n.podr.id] = { ...n.podr, children: [] };
});
data.forEach(n => podr[n.podr.id].children.push({ ...n.dolzh }));
Object.values(org).forEach(n => obl[n.parent_obl].children.push(n));
Object.values(podr).forEach(n => org[n.parent_org].children.push(n));
return Object.values(obl);
}
const tree = createTree(arr);
Array.from([...str].reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map), n => n[1] + n[0])
str.split('').sort().join('').match(/(.)\1*/g).map(n => n.length + n[0])
Array.from(new Set(str), n => str.replace(RegExp(`[^${n}]`, 'g'), '').length + n)
const walk = (el, level = 0) =>
[...el.children].reduce((p, c) => {
c = walk(c, level + 1);
return p.level > c.level ? p : c;
}, { el, level });
const getLastDeepestElement = root => walk(root).el;
const getDeepestLastElement = el =>
el.lastElementChild
? getDeepestLastElement(el.lastElementChild)
: el;
// или, без рекурсии
const getDeepestLastElement = el => Array
.from(el.querySelectorAll('*'))
.pop() || el;
firebase.auth().onAuthStateChanged(user => {
if (user) {
store.dispatch('loggedUser', user);
}
new Vue(...);
});
.on('click', function(e) {
e.stopPropagation();
<script type="text/x-template" id="tree-template"> {{ item }} </script>
li
- вы же внутри ul
пытаетесь их выводить:<script type="text/x-template" id="tree-template">
<li>{{ item }}</li>
</script>
<ul v-for="item in items" :key="item.id"> <tree-items :item="item" ></tree-items> </ul>
v-for
внутрь списка (кстати, а какого чёрта в имени компонента элемента списка множественное число? почему items, а не item?):<ul>
<tree-items
v-for="item in items"
:key="item.id"
:item="item"
></tree-items>
</ul>
function isComposite($num) {
// нет, за вас я этого делать не буду, давайте как-нибудь сами...
// в конце концов, можно и нагуглить - это дело трёх секунд
}
$newArr = array_filter($arr, 'isComposite');
const valuesShow = [ 2000, ещё какое-то значение, и ещё, и ещё, ... ];
$('#sample_form_deliv').change(function({ target: { value } }) {
$('#sample_form_pay .input-label:eq(2)').toggle(valuesShow.includes(+value));
});