const steps = [
{
name: 'Шаг 1',
component: React.lazy(() => import('./steps/step1.jsx'))
},
{
name: 'Шаг 2',
component: React.lazy(() => import('./steps/step2.jsx'))
}
];
$(document).on('keydown', '.search-auto', function (e) {});
const products = [
{
name: 'Apple',
picture: 'https://www.google.com/',
description: 'text',
quantity: 123,
price: 123,
sale: '50%',
afterSale: '400'
},
{
name: 'Jeans',
picture: 'https://www.google.com/',
description: 'text1',
quantity: 123,
price: 123,
sale: '25%',
afterSale: 4000
}
];
const isSimilar = (target, entry) => {
if (Array.isArray(target) && Array.isArray(entry)) {
if (target.length === 0 && entry.length === 0) {
return true;
}
return entry.length > 0 && entry.every(value => {
if (typeof value === 'object') {
return target.some(other => isSimilar(other, value));
}
return target.includes(value);
});
}
if (typeof target === 'object' && typeof entry === 'object') {
const targetKeys = Object.keys(target);
const entryKeys = Object.keys(entry);
if (targetKeys.length === 0 && entryKeys.length === 0) {
return true;
}
return entryKeys.length > 0 && entryKeys.every(key => {
if (typeof entry[key] === 'object') {
return isSimilar(target[key], entry[key]);
}
return target[key] === entry[key];
});
}
return target === entry;
};
const findBy = (collection, data) => collection.reduce((store, entry) => {
if (isSimilar(entry, data)) {
store.equals.push(entry);
} else {
store.other.push(entry);
}
return store;
}, { equals: [], other: [] });
console.log(findBy(products, {
"name": "Jeans",
"picture": "https://www.google.com",
"description": "text",
"quantity": 123,
"price": 123,
"sale": 123,
"afterSale": 4000,
}));
/*
{
equals: [],
other: [
{
name: 'Apple',
picture: 'https://www.google.com/',
description: 'text',
quantity: 123,
price: 123,
sale: '50%',
afterSale: '400'
},
{
name: 'Jeans',
picture: 'https://www.google.com/',
description: 'text1',
quantity: 123,
price: 123,
sale: '25%',
afterSale: 4000
}
]
}
*/
console.log(findBy(products, {
"name": "Jeans",
"picture": "https://www.google.com/",
"description": "text1",
"quantity": 123,
"price": 123,
"afterSale": 4000,
}));
/*
{
equals: [
{
name: 'Jeans',
picture: 'https://www.google.com/',
description: 'text1',
quantity: 123,
price: 123,
sale: '25%',
afterSale: 4000
}
],
other: [
{
name: 'Apple',
picture: 'https://www.google.com/',
description: 'text',
quantity: 123,
price: 123,
sale: '50%',
afterSale: '400'
}
]
}
*/
React.lazy
просто обёртка.