Replace filter with map

This commit is contained in:
Nayam Amarshe
2024-10-04 14:09:29 +05:30
parent e09d6cbd1b
commit 5ddf914290

View File

@@ -267,12 +267,11 @@ const Home = () => {
};
});
// Add newModelsList to modelOptions and remove duplicates
const combinedModelOptions = [...modelOptions, ...newModelOptions];
const uniqueModelOptions = combinedModelOptions.filter(
// Check if any model in the array appears more than once
(model, index, array) =>
array.findIndex((t) => t.value === model.value) === index,
);
const modelMap = new Map();
[...modelOptions, ...newModelOptions].forEach((model) => {
modelMap.set(model.value, model);
});
const uniqueModelOptions = Array.from(modelMap.values());
setModelOptions(uniqueModelOptions);
},
);