-1

I have 1 object (it came from localstroage). But there are some dublicates. how can i remove it from easy way?

"mainchart" is my array object.

function check() {
  for (let i1 = 0; i1 < mainchart.length; i1++) {
    for (let i2 = 0; i2 < mainchart.length; i2++) {  
      if (mainchart[i1].id === mainchart[i2].id) {
        console.log(mainchart[i1] , mainchart[i2]);
      }
    }
  }
}

check();
7
  • Does this answer your question? Counting the occurrences / frequency of array elements Commented Mar 11, 2022 at 21:05
  • it helped me thanks. but how can i count removed elements? Commented Mar 11, 2022 at 21:19
  • becouse i create shoping basket with js:) Commented Mar 11, 2022 at 21:21
  • My link contains multiple solutions to count the elements. Commented Mar 11, 2022 at 21:22
  • do you use discord? can you help me from there? Commented Mar 11, 2022 at 21:31

1 Answer 1

-1

An easy way to remove duplicate values from an array is converting the array in to a set (collection of unique values), putting the values of the set in a new array:

let array1 = ["a", "b", true, 3, 67, true, "a"];

let array2 = [...new Set(array1)];

console.log(array2); // [ 'a', 'b', true, 3, 67 ]

Hope it can help you 🦄

Sign up to request clarification or add additional context in comments.

2 Comments

"how to remove same elements from array and add count to other objects javascript?" Where do you count? Your code doesn't work with objects, only with primitives.
i want to know how many element get removed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.