let link = document.createElement('a');
link.href = "https://localhost8080/somepath/somepath/somepath?param=42#some-hash";
link.protocol // -> https:
link.host // -> localhost8080
link.pathname // -> /somepath/somepath/somepath
link.hash // -> #some-hash
link.search // -> ?param=42
Список всех свойств здесь: https://developer.mozilla.org/ru/docs/Web/API/Location $serialArr = serialize($arr);
$serialArr = json_encode($arr);
const c = a.map((i, el) => ({ selector: `#${el.id}`}));
var a = $("span[id ^= call]"),
c = [];
for (var i = 0; i < a.length; i++) {
let b = {
selector:"#" + a.eq(i).attr("id"),
};
c.push(b);
}
const a = [ 'a', 'b', 'c', 'd' ];
const b = [ 'a', 'b', 'x', 'y', 'z' ];
const c = a.filter(n => b.indexOf(n) === -1);
// или
const c = a.filter(n => !b.includes(n));
function diff(data1, data2, key = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const keys = new Set(Array.from(data2, getKey));
return Array.prototype.filter.call(data1, n => !keys.has(getKey(n)));
}
diff([1, 2, 3], [2]) // [1, 3]
diff([{id: 1}, {id: 2}, {id: 3}], [{id: 1}, {id: 3}], 'id') // [{id: 2}]
diff('AbCdE', 'aBc', n => n.toLowerCase()) // ['d', 'E']
history.pushState(null, null, null); // добавляем пустой стейт в историю
window.onpopstate = function(event) {
if (popupOpen) {
closePopUp(); // закрываем попап
}
window.onpopstate = null; // удаляем обработчик события перехода по истории
};
$(function() {
function getRandomElems($el) {
$("#b").html("");
var $li = $el.siblings();
$li.length < 3 && ($li = $li.add($el));
$li = $li.get();
for (var i = 0; i < 3; i++) {
var rand = (Math.random() * $li.length) | 0;
$($li.splice(rand,1)).clone().appendTo($("#b"));
}
}
$("#a").on('click', 'li', function() {
getRandomElems($(this));
});
});
const sourceSelector = '#a';
const targetSelector = '#b';
const itemSelector = 'li';
const count = 3;
const random = arr => arr.splice(Math.random() * arr.length | 0, 1)[0];
$(sourceSelector).on('click', itemSelector, function() {
const items = $(this).siblings().clone().get();
$(targetSelector).html([...Array(count)].map(random.bind(null, items)));
});
// или
const source = document.querySelector(sourceSelector);
const target = document.querySelector(targetSelector);
source.addEventListener('click', ({ target: t }) => {
if (t = t.closest(itemSelector)) {
const items = [...source.querySelectorAll(itemSelector)].filter(n => n !== t);
target.innerHTML = '';
target.append(...Array.from(
{ length: Math.min(items.length, count) },
() => random(items).cloneNode(true)
));
}
});
если в списке всего три элемента - все три и выводятся
const items = $(itemSelector, sourceSelector).clone().get();
if (items.length > count) {
items.splice($(this).index(), 1);
}
// или
const items = [...source.querySelectorAll(itemSelector)];
if (items.length > count) {
items.splice(items.indexOf(t), 1);
}
<div id="list">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div>
const $items = $('#list .item').click(function() {
console.log($items.not(this).slice(0, 3).toArray());
});
// или
$('#list').on('click', '.item', function() {
console.log($(this).siblings().slice(0, 3).get());
});