let finalRes = item.number + "";
const arr = [
{ name: "А" },
{ code: 1, name: "Адыгея, Респ.", capital: "Майкоп" },
{ code: 104, name: "Азербайджан", capital: "" },
{ name: "Б" },
{ code: 2, name: "Башкортостан, Респ.", capital: "Уфа" },
{ code: 3, name: "Таганрог", capital: "" },
{ code: 5, name: "Сочи", capital: "" },
{ code: 105, name: "Беларусь", capital: "" },
{ name: "В" },
{ code: 33, name: "Владимирская обл.", capital: "Владимир" },
{ code: 34, name: "Волгоградская обл.", capital: "Волгоград" },
{ code: 35, name: "Вологодская обл.", capital: "Вологда" }
];
for (let i = 0; i < arr.length; i++) {
if (!arr[i].code) continue;
if (!arr[i - 1].code) {
arr[i].position = "top";
} else if (i + 1 === arr.length || !arr[i + 1].code) {
arr[i].position = "bottom";
} else {
arr[i].position = "center";
}
}
console.log(arr);
[...new Map(arr.map((item) => [item["key"], item])).values()]
const uniqueIds = [];
arr.filter(element => {
const isDuplicate = uniqueIds.includes(element.key);
if (!isDuplicate) {
uniqueIds.push(element.key);
return true;
}
return false;
});
Object.values(
arr.reduce( (c, e) => {
if (!c[e.key]) c[e.key] = e;
return c;
}, {})
);
const unique = (arr, key) => {
const keys = new Set();
return arr.filter(el => !keys.has(el[key]) && keys.add(el[key]));
};
unique(arr, 'key')
срабатывает дважды, а в последующие разы увеличивается в разы
useEffect(() => { window.addEventListener('keydown', onKeyPress); }, [focus])
useEffect(() => {
const onKeyDown = ({ keyCode }) => {
if (keyCode === 39) {
setFocus(focus => focus + 1);
}
};
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, []);
<img src={star} alt="star" onClick={() => addFavoriteChanel(index)} id={index} className={`star ${favorite <b>=== index</b> ? 'active' : ''}`} />
const [favorite, setFavorite] = useState(0);
const addFavoriteChanel = (index) => {
if(favorite === index) {
setFavorite(0);
} else {
setFavorite(index)
}
}