const sumNested = (data, getVal, key) => Object
.entries(data instanceof Object ? data : {})
.reduce((acc, [ k, v ]) => acc + sumNested(v, getVal, k), getVal(key, data));
const numItems = sumNested(obj, (k, v) => (k === 'items' && Array.isArray(v)) ? v.length : 0);function sumNested(data, getVal) {
let result = 0;
for (const stack = [ [ , data ] ]; stack.length;) {
const [ k, v ] = stack.pop();
stack.push(...(v instanceof Object ? Object.entries(v) : []));
result += getVal(k, v);
}
return result;
}
final_data.reduce((acc1, num1) => acc1.reduce((acc2, num2) => acc2.concat(num1.map(num3 => [].concat(num2, num3))), []))
final_data.reduce((acc1, num1) => {
return acc1.reduce((acc2, num2) => {
acc2.push(...num1.map(num3 => [...num2, ...num3]));
return acc2;
}, [])
})return final_data.reduce((acc1, num1) =>
acc1.reduce((acc2, num2) => (
acc2.push(...num1.map(num3 => num2 + num3)),
acc2
), [])
).join(',');
const whatIsInAName = (collection, source) =>
collection.filter(function(n) {
return this.every(([ k, v ]) => n[k] === v);
}, Object.entries(source));function whatIsInAName(collection, source) {
const result = [];
COLLECT:
for (const n of collection) {
for (const k in source) {
if (source.hasOwnProperty(k) && n[k] !== source[k]) {
continue COLLECT;
}
}
result.push(n);
}
return result;
}
const parentSelector = '.wrap';
const wrapperTag = 'div';
const wrapperClass = 'wrap-item';
const wrapSize = 4;const $items = $(parentSelector).children();
for (let i = 0; i < $items.length; i += wrapSize) {
$items.slice(i, i + wrapSize).wrapAll(`<${wrapperTag} class="${wrapperClass}">`);
}const parent = document.querySelector(parentSelector);
parent.append(...Array.from(
{ length: Math.ceil(parent.children.length / wrapSize) },
() => {
const wrapper = document.createElement(wrapperTag);
wrapper.classList.add(wrapperClass);
wrapper.append(...Array.prototype.slice.call(parent.children, 0, wrapSize));
return wrapper;
}
));
const text = document.querySelector('#area').value;
const pre = document.querySelector('#pre');
pre.innerHTML = text.replace(/f+/g, '<span>$&</span>');
document.addEventListener('scroll', function() {
document.querySelector('.wrap_portfolio').style.transform = `translateX(${window.scrollY}px)`;
});
const color = 'orange';.const { id = null } = Object.values(fruits).find(n => n.color === color) || {};
// или
let id = null;
for (const k in fruits) {
if (fruits.hasOwnProperty(k) && fruits[k].color === color) {
id = fruits[k].id;
break;
}
}А если у меня несколько оранжевых фруктов, как поступить?
const ids = Object.values(fruits).reduce((acc, n) => (
n.color === color && acc.push(n.id),
acc
), []);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;
}const idsByColor = group(Object.values(fruits), 'color', 'id');
const orangeIds = idsByColor.orange || [];
const greenIds = idsByColor.green || [];
id="0" ---> data-index="0".const imgSelector = '#main__display img';
const key = 'index';
const attr = `data-${key}`;
const buttonSelector = `[${attr}]`;const getSrc = el => collect[
$(el).data(key)
// или
$(el).attr(attr)
// или
el.dataset[key]
// или
el.getAttribute(attr)
// или
el.attributes[attr].value
];$(buttonSelector).click(function(e) {
$(imgSelector).attr('src', getSrc(this));
});
// или
$(document).on('click', buttonSelector, e => {
$(imgSelector).prop('src', getSrc(e.currentTarget));
});
// или
document.querySelectorAll(buttonSelector).forEach(function(n) {
n.addEventListener('click', this);
}, function(e) {
this.setAttribute('src', getSrc(e.currentTarget));
}.bind(document.querySelector(imgSelector)));
// или
document.addEventListener('click', ({ target: t }) => {
if (t = t.closest(buttonSelector)) {
document.querySelector(imgSelector).src = getSrc(t);
}
});
const THICKNESS = {
thinLine: 1,
get midLine() {
return this.thinLine * 2;
},
get boldLine() {
return this.thinLine * 4;
},
};
после 21 итерации выдает неправильное значение даты
const startDate = new Date(2019, 7, 11);
const currentDate = new Date(startDate);
for (let i = 0; i <= 30; i++) {
currentDate.setDate(currentDate.getDate() + 1);
console.log(currentDate);
}const startDate = new Date(2019, 7, 11);
for (let i = 0; i <= 30; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
console.log(currentDate);
}
function createList(data) {
const list = document.createElement('ul');
const stack = [];
let [ ul, level ] = [ list, +/\d+/.exec(data[0]) ];
for (const n of data) {
const currLevel = +/\d+/.exec(n);
if (currLevel > level) {
stack.push([ ul, level ]);
[ ul, level ] = [ document.createElement('ul'), currLevel ];
stack[stack.length - 1][0].lastChild.append(ul);
} else {
for (; currLevel < level && stack.length; [ ul, level ] = stack.pop()) ;
}
ul.append(document.createElement('li'));
ul.lastChild.textContent = n;
}
return list;
}
изучаю метод фильтр
greater than or equal to 8.0
rating > 8combination of filter and map
watchList.filter(n => n.imdbRating >= 8).map(n => ({ title: n.Title, rating: n.imdbRating }))
.one path {
fill: #909090;
}
.one.active path {
fill: #f00;
}const itemSelector = '.one';
const activeClass = 'active';document.addEventListener('click', ({ target: t }) =>
(t = t.closest(itemSelector)) && t.classList.toggle(activeClass)
);
// или
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => e.currentTarget.classList.toggle(activeClass));