const key = 'workplace';
const values = [ 'office', 'hotel' ];
const result = arr.filter(function(n) {
return this.has(n[key]);
}, new Set(values));
// или
const result = values.flatMap(((grouped, n) => grouped[n] ?? []).bind(
null,
arr.reduce((acc, n) => ((acc[n[key]] = acc[n[key]] ?? []).push(n), acc), {})
));
// или
const result = [];
for (const n of arr) {
for (const m of values) {
if (m === n[key]) {
result.push(n);
break;
}
}
}
// или
const result = [];
for (let i = 0; i < arr.length; i++) {
if (~values.indexOf(arr[i][key])) {
result[result.length] = arr[i];
}
}
// или
const result = (function get(i, n = arr[i]) {
return n
? [].concat(values.includes(n[key]) ? n : [], get(-~i))
: [];
})(0);
search = [ 'White', 'Black', 'Asian', 'Hispanic' ]
max = arr
.select{|n| search.include?(n['description'])}
.max_by{|n| n['count']}
<div>
<span>{{ item.questions[index]['description'] }}</span>
<span v-if="item.questions[index]['answer'] == 2">Да</span>
<span v-if="item.questions[index]['answer'] == 1">Нет</span>
</div>
<div v-for="n in item.questions">
<span>{{ n.description }}</span>
<span>{{ n.answer | answer }}</span>
</div>
filters: {
answer: val => [ 'Нет', 'Да' ][val - 1],
...
#list-case
вы назначаете обработчики клика всем существующим .img
и .show
, а не только свежесозданным. Если срабатывает два (или четыре, или шесть, или...) обработчика, которые выполняют toggleClass, то конечное состояние от начального отличаться не будет. Отсюда ваше кажущееся "не работает"..case
, делайте это один раз, используя делегирование:$('#list-case')
.on('click', '.img', function() {
$(this).closest('.case').remove();
})
.on('click', '.show', function() {
$(this).toggleClass('none').closest('.case').find('.case-bottom').toggleClass('invise');
});
const placeholders = [ 'hello, world!!', 'fuck the world', 'fuck everything' ];
const delay = 200;
const input = document.querySelector('input');
function interval(arr, delay, callback) {
let i = -1;
return arr.length
? setInterval(() => callback(arr[i = -~i % arr.length]), delay)
: null;
}
const intervalId = interval(placeholders, delay, n => input.placeholder = n);
// хотим остановить, делаем так: clearInterval(intervalId);
function interval(arr, delay, callback) {
let timeoutId = null;
arr.length && (function next(i) {
timeoutId = setTimeout(() => {
callback(arr[i]);
next((i + 1) % arr.length);
}, delay);
})(0);
return () => clearTimeout(timeoutId);
}
const stop = interval(
placeholders,
delay,
Element.prototype.setAttribute.bind(input, 'placeholder')
);
// хотим остановить, делаем так: stop();
render: val => val
? <Tag color="success">Активен</Tag>
: <Tag color="default">Не активен</Tag>
return false;
toggle дважды начинает срабатывать
const comparators = [
[ 'city', (itemVal, filterVal) => itemVal === filterVal ],
[ 'title', (itemVal, filterVal) => itemVal.includes(filterVal) ],
[ 'type', (itemVal, filterVal) => itemVal.includes(filterVal) ],
];
const filteredArr = arr.filter(n => comparators.every(([ k, f ]) => f(n[k], filter[k])));
document.querySelectorAll('.group-options__item').forEach(n => {
const input = (n.closest('.group-options') ?? n).querySelector('[data-prop]');
if (input) {
input.checked = true;
}
});