condition: {
exclude: [{disabled: true}, {rating: 24}]
}
}
const { condition, data } = users;
const commonCondition = condition
.exclude
.reduce((acc, condition) => ({...acc, ...condition }), {});
const filteredUsers = data.filter((user) => { // [{"user": "Dmitry", "rating": 22, "disabled": false}]
return Object
.entries(commonCondition)
.every(([key, value]) => value !== user[key])
});
const condition = {
exclude: [{
disabled: true
}],
include: [{
rating: 24
}]
}
const getFiltered = (data, filters) => {
const {
include,
exclude
} = filters;
let filteredData = data;
if (include) {
const includeEntries = include.flatMap(filter => Object.entries(filter));
filteredData = filteredData.filter(value =>
includeEntries.every(([key, valueFilter]) => value[key] === valueFilter)
);
}
if (exclude) {
const excludeEntries = exclude.flatMap(filter => Object.entries(filter));
filteredData = filteredData.filter(value =>
excludeEntries.every(([key, valueFilter]) => value[key] !== valueFilter)
);
}
return filteredData;
}
const index = 1;
const count = 2;
const value = 'hello, world!!';
arr.splice(index, count, ...Array(count).fill(value));
// или, splice использовать необязательно
for (let i = count; i-- > 0; arr[index + i] = value) ;
const fixCount = Math.max(0, Math.min(count, arr.length - index));
Аккумулятором ведь выступает 1 объект в этом массиве.
{}
[ {id: 'a'}, {id: 'b'}, {id: 'c'} ]
{
a: {id: 'a'},
b: {id: 'b'},
c: {id: 'c'}
}