0

I'm trying to delete duplicate letters in each multidimensional array but am having trouble with the syntax between a single array vs a multidimensional array. I can get it to work for a single array as follows:

function uniq(a) {
 return Array.from(new Set(a)) 
}
// uniq([8,7,8]) successfully returns [8,7]

But it would not work for code like this:

uniq([[8,7,8],[6,8]])

How could this be achieved? Similarly I was trying to make a simple function that just increases the MD array values by 1 but that did not work either:

[[4,6,1],[4,9]].map(function(c,i,a){ return c[i+1] });

There are similar questions like this one, but in my case, each multidimensional array is different, and it's the letters in those MD arrays I want to remove duplicates from. Thanks for any help here.

user8758206
  • 2,106
  • 4
  • 22
  • 45

2 Answers2

2

You can use your existing function and try following.

function uniq(a) {
   return Array.from(new Set(a)) 
}
console.log([[8,7,8],[6,8]].map(uniq));

Similarly, for the addition by 1, you can try following

console.log([[4,6,1],[4,9]].map(a => a.map(v => v+1)));
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

let arrayWithUnique1DArray = [[4,6,4],[4,9,9]].map((item)=>{ 
  return [...(new Set(item))] 
});
console.log(arrayWithUnique1DArray)

function increaseEachValueBy({arrays, value}={}){
  arrays.forEach((array, index, original)=> { original[index] = array.map(item => item+ value)})
}

let TwoDimensionArray = [[4,6,4],[4,9,9]]

increaseEachValueBy({ arrays: TwoDimensionArray , value:10})
console.log(TwoDimensionArray)
Mohammed Ashfaq
  • 3,353
  • 2
  • 15
  • 22