how to delete repeated data points in javascript

One way to delete repeated data points in a JavaScript array is to create a new array and loop through the original array, adding elements to the new array only if they haven't been added before. Here's an example implementation:

index.tsx
function removeDuplicates(array) {
  var newArray = [];
  for (var i = 0; i < array.length; i++) {
    if (newArray.indexOf(array[i]) === -1) {
      newArray.push(array[i]);
    }
  }
  return newArray;
}
206 chars
10 lines

You can then call this function for any array you want to remove duplicates from:

index.tsx
var myArray = [1, 2, 3, 3, 4, 5, 5, 5];
var myNewArray = removeDuplicates(myArray);
console.log(myNewArray); // outputs [1, 2, 3, 4, 5]
136 chars
4 lines

This implementation uses the indexOf method to check if an element has already been added to the new array. If it hasn't, it gets added with the push method. The function returns the new array with duplicates removed.

related categories

gistlibby LogSnag