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.

0

2 Answers 2

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)));

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.