1

How to delete the dublicate value from Row? my array look like this

let array =[["a"],["b"],["c"],["a"],["b"],["c"]];

5 Answers5

1

const array =[["a"],["b"],["c"],["a"],["b"],["c"]]; 

const resultSet = new Set();
const resultArr = [];

array.forEach(items => {
  items.forEach(item => {
      resultSet.add(item);
  });
});

for(const item of resultSet) {
    resultArr.push([item]);
}

console.log(resultArr);
sasi66
  • 437
  • 2
  • 7
1

There's this... use new Set and spread after breaking apart the array, then stitch it back together

[...new Set(array.join(''))].map(m=>[m])

const array =[["a"],["b"],["c"],["a"],["b"],["c"]]; 
let filtered = [...new Set(array.join(''))].map(m=>[m])
console.log(filtered)
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

You should group by a property. For example, the first item in the array.

let array =[["a"],["b"],["c"],["a"],["b"],["c"]]; 

var result = Object.values(array.reduce(function(agg, item) {
  var key = item[0]
  agg[key] = agg[key] || item
  return agg
}, {}))

console.log(result)
IT goldman
  • 14,885
  • 2
  • 14
  • 28
0

Reasonably fast way to compare them by value in one line.

let array =[["a"],["b"],["c"],["a"],["b"],["c"]]; 
const result = Array.from(new Set(array.map(JSON.stringify)), JSON.parse);
console.log(result);

See also Remove Duplicates from JavaScript Array for other approaches

Chris G
  • 1,598
  • 1
  • 6
  • 18
0

You can also use the flat method to aid in removing duplicate elements.

let array =[["a"],["b"],["c"],["a"],["b"],["c"]];


const set = [...new Set(array.flat())].map(x => [...x]);
console.log(set); // [['a'], ['b'], ['c']]
Juan
  • 477
  • 4
  • 8