-2

It is not a duplicate! The link above doesn't show how to delete all other subarrays values by index of the third subarray (see expected array).

This code finds duplicates:

    var array = [];
    
    array[0] = [0, "1:7001", "1:7002", "1:7003", "1:7004", "1:7005"];
    array[1] = [1, "1:8001", "1:8002", "1:8003", "1:8004", "1:8005"];
    array[2] = ["asw", 32884789084, 32919691801, 32919691801, 32919691801, 32857764298];
    
    var dup = array[2].filter((v, i, a) => a.indexOf(v) !== i);
    
    console.log(dup);

But I don't know how to delete elements in subarrays. Expected array should look like:

[0, "1:7001", "1:7002", "1:7005"]; 
[1, "1:8001", "1:8002", "1:8005"];
["asw", 32884789084, 32919691801, 32857764298];
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
F. Vosnim
  • 476
  • 2
  • 8
  • 23

1 Answers1

2

You can do it like this with help of map and Set

So with map here i am iterating each element of array variable. and than making a set out of each subarray (which means it will have only unique items). and before returning i am using spread operator to de-structure.

var array = [];
    
    array[0] = [0, "1:7001", "1:7002", "1:7003", "1:7004", "1:7005"];
    array[1] = [1, "1:8001", "1:8002", "1:8003", "1:8004", "1:8005"];
    array[2] = ["asw", 32884789084, 32919691801, 32919691801, 32919691801, 32857764298];
    
    
    var dup = array.map(e=>[...new Set(e)])
    
    console.log(dup);

If you want to remove elements from other subarray based on 3rd subarray you can do it like this.

var array = [];
     
array[0] = [0, "1:7001", "1:7002", "1:7003", "1:7004", "1:7005"];
array[1] = [1, "1:8001", "1:8002", "1:8003", "1:8004", "1:8005"];
array[2] = ["asw", 32884789084, 32919691801, 32919691801, 32919691801, 32857764298];
        
let op = array.map((e,ind,a)=>{
  return e.filter((el,i)=>{
    return  i == 0 ? true : a[2][i] !== a[2][i-1]
  })
})
        
        console.log(op);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60