Por ejemplo,
[{ "numberStart": "300", "numberEnd": "350", "id": "1" }, { "numberStart": "351", "numberEnd": "400", "id": "2" }, { "numberStart": "380", "numberEnd": "400", "id": "3" }]En el ejemplo anterior, el tercer elemento de la matriz está duplicado porque los rangos numberStart y numberEnd ya existen en el segundo elemento de la matriz. ¿Cómo encontrar el elemento duplicado?
Suponga que comienza con una lista vacía y continúa agregando un rango de objetos.
Una posible solución es la siguiente. Los nuevos objetos se agregarán solo si no se superponen al rango máximo de los elementos existentes.
En el siguiente ejemplo, solo los objetos 1, 2 y 5 se agregarán a la matriz.
let list = []; // start with an empty list //-------------------------------- function addToList(list , toAdd) { const maxValue = Math.max(...list.map(o => o.numberEnd), 0); if (toAdd.numberEnd > maxValue) list=[{...list,...toAdd}]; return list; } //-------------------------------- list = addToList(list,{"numberStart": 300,"numberEnd": 350, "id": "1"}); // will be added list = addToList(list,{"numberStart": 351,"numberEnd": 400, "id": "2"}); // will be added list = addToList(list,{"numberStart": 380, "numberEnd": 400, "id": "3"}); // it will not be added list = addToList(list,{"numberStart": 200, "numberEnd": 300, "id": "4"}); // it will not be added list = addToList(list,{"numberStart": 401, "numberEnd": 500, "id": "5"}); // will be added console.log(list);